JavaScript Array sort()
JavaScript Array sort()
The sort() method is used to sort the elements of an array. It will overwrite the original array. It sorts the elements as strings in alphabetical and ascending order.
Syntax:-
Its syntax is:- array.sort(compareFunction)
Return:-
Its return is:- the array with the items sorted.
Further Explanation:-
Parameter | Description |
compareFunction | Optional. A function that defines a sort order. For example, function(a, b){return b-a}. This function should return a negative, zero, or positive value, depending on the arguments. When sort() compares two values, it sends the values to the compare function, and sorts the values according to the returned value. |