Example 1: javascript sort array with objects
var array = [
{name: "John", age: 34},
{name: "Peter", age: 54},
{name: "Jake", age: 25}
];
array.sort(function(a, b) {
return a.age - b.age;
});
Example 2: sorting array from highest to lowest javascript
let numbers = [5, 13, 1, 44, 32, 15, 500]
let lowestToHighest = numbers.sort((a, b) => a - b);
let highestToLowest = numbers.sort((a, b) => b-a);
Example 3: how to sort an array by value in javascript
arr.sort(function(a, b){return a-b});
arr.sort((a, b) => a - b);
arr.sort(function(a, b){return b - a});
arr.sort((a, b) => b - a);
Example 4: sort javascript array
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)
Example 5: sort array of objects javascript by value
let orders = [
{
order: 'order 1', date: '2020/04/01_11:09:05'
},
{
order: 'order 2', date: '2020/04/01_10:29:35'
},
{
order: 'order 3', date: '2020/04/01_10:28:44'
}
];
console.log(orders);
orders.sort(function(a, b){
let dateA = a.date.toLowerCase();
let dateB = b.date.toLowerCase();
if (dateA < dateB)
{
return -1;
}
else if (dateA > dateB)
{
return 1;
}
return 0;
});
console.log(orders);
Example 6: javascript sort function
var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)