Javascript array sort() is the inbuilt method that sorts all the items of the array in place and returns that array. The sort() method can also accept the parameter like function. So it will sort an array based on that function logic. The default sort order is built on converting the item into strings and then comparing their sequences of UTF-16 code unit values.

Javascript array sort example

The sort order can be either an alphabetic or the numeric, and either the ascending (up) or descending (down). Javascript array sort() method is not a pure function because the sort() method changes the original array.

Syntax

array.sort(compareFunction)

The compareFunction_() _parameter is an optional argument. The function that defines an alternative sort order and it should return a negative, zero, or positive value, depending on the arguments.

#javascript #js array sort

Javascript Array Sort Example | JS Array sort() Method
1.75 GEEK