js array sort by value code example
Example 1: 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 2: javascript sort by id
elems.sort((a, b) => a.id - b.id);
Example 3: array sort js
arr = ['width', 'score', done', 'neither' ]
arr.sort()
arr.sort((a,b) => a.localeCompare(b))
arr = [21, 7, 5.6, 102, 79]
arr.sort((a, b) => a - b)
Example 4: sort array of objects in ascending order in js
homes.sort(function(a, b) {
return parseFloat(a.price) - parseFloat(b.price);
});