Sorting Arrays of Strings
In JavaScript arrays have a sort( ) method that sorts the array items into an alphabetical order.
The following illu...
For further actions, you may consider blocking this person and/or reporting abuse
There's a big caveat to this method of string sorting: it doesn't take Unicode and non-English alphabets into account. Depending on your application, you might want to consider using the
String::localeCompare()
instead which has built-in support for things like language-specific sort ordering, ignoring cases or diacritics:Love this reply. It's exactly what I was looking for. I do want to comment that there is a typo in case someone else tries to use this like I did and it didnt work. In the localeSort variable assignment it should be
return a.localeCompare(b, 'en', { sensitivity: 'base' });
Thanks! both anwser are awesome!
thank you.
I know it is an extension of the topic... How can I add an argument to the callback-function? I want to sort Objects according to their properties.
example of not working dummy code:
var sortprop='weight';
animals.sort(function (a, b, sortprop){....});
Use a compare closure function.
thank you.