sort in array javascript code example

Example 1: sort javascript array

var points = [40, 100, 1, 5, 25, 10];
points.sort((a,b) => a-b)

Example 2: sorting of arraray's number element in javascript

let numbers = [0, 1, 2, 3, 10, 20, 30];
numbers.sort((a, b) => a - b);

console.log(numbers);

Example 3: sort js array array

var ages = [18, 21, 9, 41, 35, 24]
ages.sort(function(a, b) {
  return a - b
})
// => [9, 18, 21, 24, 35, 41]

Example 4: sort function explained javascript

var sortedArray = myArray.sort(function(a,b){
                                   if (a.name < b.name)
                                      return -1;
                                   else if (a.name == b.name)
                                      return 0;
                                   else
                                      return 1;
                               });

Example 5: mdn array sort

arr.sort([compareFunction])