You say the comparison is for sorting. Then I suggest instead:
"a".localeCompare("b");
It returns -1 , since "a" < "b" , 1 or 0 otherwise, as you need for Array.prototype.sort ()
Keep in mind that sorting is language dependent. For example. in German, Γ€ is a variant of a , so "Γ€".localeCompare("b", "de-DE") returns -1 . In Swedish, Γ€ is one of the last letters in the alphabet, so "Γ€".localeCompare("b", "se-SE") returns 1 .
Without the second localeCompare parameter, the browser locale is used. Which in my experience is never what I want, because then it will be sorted differently than a server that has a fixed locale for all users.
Peter V. MΓΈrch Jan 13 '16 at 23:38 2016-01-13 23:38
source share