javascript array sort by value code example
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: javascript sort alphabetically
var items = ['réservé', 'premier', 'communiqué', 'café', 'adieu', 'éclair'];
items.sort(function (a, b) {
return a.localeCompare(b);
});
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: javascript sort
homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Example 5: javascript sort array in ascending order
function myFunction() {
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points;
}