array.sort((a,b) => new Date(b.date) - new Date(a.date));
// Price Low To High
array?.sort((a, b) => (a.price > b.price ? 1 : -1))
// Price High To Low
array?.sort((a, b) => (a.price > b.price ? -1 : 1))
// Name A to Z
array?.sort((a, b) => (a.name > b.name ? 1 : 1))
// Name Z to A
array?.sort((a, b) => (a.name > b.name ? -1 : 1))
// Sort by date
array.sort((a,b) => new Date(b.date) - new Date(a.date));
array.sort((a,b) => new Date(b.date) - new Date(a.date));
array.sort((a,b)=>a.getTime()-b.getTime());
//This method will be sort array in ascending order:
var numArray = [5, 10, 12, 9, 31, 21, 18, 55, 39, 40];
numArray.sort((a, b) => {
return a - b;
});
console.log(numArray);
//This method will be sort array in descending order:
numArray.sort((a, b) => {
return b - a;
});
console.log(numArray)
//sort array by date
// Name Z to A
array.sort((a, b) => (a.name > b.name ? -1 : 1))
console.log(array)
// Sort by date
array.sort((a,b) => new Date(a.date) - new Date(b.date));
console.log(array)
array.sort(function(a,b){return a.getTime() - b.getTime()});
array.sort(function(o1,o2){
if (sort_o1_before_o2) return -1;
else if(sort_o1_after_o2) return 1;
else return 0;
});
var l = [new Date(2022, 10, 10), new Date(2022, 10, 8), new Date(2022, 10, 6), new Date(2022, 10, 15), new Date(2022, 10, 2)];
console.log(l.sort((date1, date2)=>date1-date2));