The sort() method is one of the strongest array methods.
The sort() method sorts an array alphabetically.
By default, the sort() function sorts values as strings not for numbers.
var friends = ["Ban", "Jane", "Alex", "Matt"];
fruits.sort(); // Sorts the elements of friends
fruits.reverse(); // Reverses the order of the elements
Note: sort() method only sorts string values only not numbers you can fix that issue by following below trick:
You can fix this by providing a compare function:
this function wil sort the array in assending order:
var points = [50, 100, 1, 6, 24, 10];
points.sort(function(a, b){return a - b});
Use the same trick to sort an array descending:
var points = [50, 100, 1, 6, 24, 10];
points.sort(function(a, b){return b - a});