JVerstry JVerstry - 1 year ago 124
Javascript Question

How to perform case insensitive sorting in Javascript?

I have an array of strings I need to sort in Javascript, but in a case insensitive way. How to perform this?

Answer Source

In (almost :) a one-liner

["Foo", "bar"].sort(function (a, b) {
    return a.toLowerCase().localeCompare(b.toLowerCase());

Which results in

[ 'bar', 'Foo' ]


["Foo", "bar"].sort();

results in

[ 'Foo', 'bar' ]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download