The sort( ) method accepts a function that compares two items of the Array.sort([comparer]) .
The sort( ) method will sort the item based on the values of the elements in case the compare function is omitted:
Suppose you have two elements a and b and want to compare a.firstname and b.firstname, then act as follows:
If compared strings contain Unicode characters, you can use localeCompare function which has built-in support for language-specific sort ordering, ignoring cases, diacritics, symbols like ß:
The sort( ) method sorts the array elements in place and returns the sorted array. By default, the sort order is ascending, built upon converting the elements into strings and then comparing their sequences of UTF-16 code unit values. Number sorting can be incorrect as the sort( ) method sorts numbers in the following order: "35" is bigger than "225", because "3" is bigger than "2".
The localeCompare() method compares two strings in the current locale. It returns a number which indicates whether an element comes before, after, or is equal to the given string in sort order.