Learn JavaScript
Advanced Array Methods


The sort() method sorts an array. By default, the method sorts the array in the ascending order.

The sorting happens according to the order in which the string versions of the array elements will appear in a dictionary.

As you can see, because the sort method looks at the array elements as strings, 11 comes before 2 in the code given below.

We can supply a function as an argument to the sort() method. We can even change the sort order from ascending to descending.

In the example given above, the sortNumbers function compares two elements of the array in the specified manner.