Having some slight issues trying to get underscore.js to do case-insensitive sorting. I have an array of objects and would like to be able to sort by property name.
Don't use _.sortBy for this. The correct way to sort strings alphabetically is to use localeCompare. Here's an example in pure Javascript:
['Z', 'A','z','á', 'V'].sort(function(a, b){
return a.localeCompare(b, undefined /* Ignore language */, { sensitivity: 'base' })
});
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare.
The name to sort by can be the field name OR a function, so pass a function that does a lower-case conversion.
var sorted = _.sortBy(array, function (i) { return i.name.toLowerCase(); });
should do the trick.
Chris' answer worked well for me, and I made it a little shorter with an arrow function:
var sorted = _.sortBy(array, (i) => i.name.toLowerCase());