We look at the default
Array.prototype.sort behavior and discuss how you can do case insensitive string sorting.
Here, we have an array, foo, of a few strings. The strings contain mixed case, for example Alpha with a capital A, beta with a small B, gamma with a capital G, and delta with a small D. By default, if I call the sort method on this array and log out the sorted array, you will see that all the capital words appear before the lowercase ones.
To do case-insensitive sorting, we need to provide a custom compare function to array prototype sort. We simply use a function that takes two strings, and uses the localeCompare function of the first string to compare the string to the second one, and return it.
Now, there are a few ways of ways adding case-insensitivity to this, but the simplest one with the greatest browser support is to simply convert both strings to lowercase before comparing them using localeCompare. Now, if we run the code, you can see that the strings are compared case-insensitively.