array sort method js code example

Example 1: sort javascript array

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

Example 2: javascript sort

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]

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: javascript sort

homes.sort(function(a, b) {
    return parseFloat(a.price) - parseFloat(b.price);
});

Example 5: JavaScript Sorting Arrays

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();        // Sorts the elements of fruits

Tags:

Misc Example