Using sort() method you can define the sorting in an array. Returns the reference to the same array, the sort() method was applied but this time sorted. The default sort order is ascending, but a custom ordering rules can be applied using a compare function.

The compare function specifies a function that defines how the array will be sorted. It accepts to arguments (a, b) which are the first and second elements for comparison.