Tri de table js
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
Worried Wildebeest
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
let numbers = [0, 1, 2, 3, 10, 20, 30];
numbers.sort((a, b) => a - b);
console.log(numbers);
var items = [
{ name: 'Edward', value: 21 },
{ name: 'Sharpe', value: 37 },
{ name: 'And', value: 45 },
{ name: 'The', value: -12 },
{ name: 'Magnetic', value: 13 },
{ name: 'Zeros', value: 37 }
];
// sort by value
items.sort(function (a, b) {
return a.value - b.value;
});
// sort by name
items.sort(function(a, b) {
var nameA = a.name.toUpperCase(); // ignore upper and lowercase
var nameB = b.name.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
/*//*\[email protected]________________/|*\*/
/**/ Array.prototype.sort2d = function(column) { //
/**/ column = typeof column === "undefined" ? 0 : column //
/**/ return this.sort((a, b) => a[column]-b[column]); } //
/**/ const unsorted2DArray = [[3,1,1,1],[9,3,3,3],[6,2,2,2]] //
/**/ sorted2DArray = unsorted2DArray.sort2d() //
/**/ //sorted2DArray = [ [3,1,1,1],[6,2,2,2],[9,3,3,3] ] //
/*//*\======================/*//*/=========================/||\*/
let numbers = [4, 10, 5, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers);
// [1, 2, 3, 4, 5]
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort