xxxxxxxxxx
29
let arr = ["def", "abc", "ghi"];
let arr2 = [{name: "def"}, {name: "abc"}, {name: "ghi"}];
let arr3 = [{id: 23}, {id: 15}, {id: 5}];
function setup() {
// Words
console.log(arr);
arr.sort();
console.log(arr);
// Objects with text
console.log(arr2);
arr2.sort();
console.log(arr2);
// Objects with ids
console.log(arr3);
arr2.sort(compareObject);
console.log(arr3);
// Maybe if that would be confusing it could be this:
// arr2.sortObjects();
// arr3.sortObjects();
}
let compareObject = function(a,b) {
return a.id > b.id;
}