xxxxxxxxxx
// ascending (normal)
numArray.sort((a,b) => a-b);
// decending (reverse)
numArray.sort((a,b) => b-a);
xxxxxxxxxx
const bignumbers = [66, 58, 81, 444, 92, 9, 6, 13, 2];
const sortedNumbers = bignumbers.sort(function (a, b) {
return a - b;
})
console.log(sortedNumbers);
//Output: [2, 6, 9, 13, 58,66, 81, 92, 444]
xxxxxxxxxx
objs.sort((a,b) => a.last_nom - b.last_nom); // b - a for reverse sort
xxxxxxxxxx
//sort is function in js which sort according to alphanumerical
//for array with number it is little twist
const items= ["Banana","Orange","Apple"];
const ratings = [92,52,2,22]
console.log(items.sort())// reuturn ["Apple","Banana","Orange]
//for array with number
ratings.sort(function(a,b){
return a-b; //ascending for decending b-a
// return is negative a is sorted befor b
// positive b is sorted before a
// if they are the same is 0 then nothing changes.
})
xxxxxxxxxx
const numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
// [1, 2, 3, 4, 5]
xxxxxxxxxx
var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
return a - b;
});
console.log(numArray);
Run code snippet