Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore.js Case Insensitive Sorting

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.


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.