var points = [40, 100, 1, 5, 25, 10]; function myFunction() { points.sort(function(a, b){ return a - b }); } console.log(myFunction()); how to run it in console.log code example
Example: function(a, b){return b - a}
The sort() method needs to know the relation between each two elements in order to sort them.
For each pair (according to the algorithm used) it calls the function then based on the return value,
may swap them.
Note that a-b returns a negative value if b>a and a positive one if a>b.
That leads to an ascending order.
Just for test, replace a-b with b-a and you will get a descending order.
You may even make it more complicated,
sort them based on (for example) their least significant digit:
a.sort(function() {return (a%10) - (b%10);}