Skip to content

Instantly share code, notes, and snippets.

@reicolina
Created November 7, 2015 15:41
Show Gist options
  • Save reicolina/82934b60efe885d45da1 to your computer and use it in GitHub Desktop.
Save reicolina/82934b60efe885d45da1 to your computer and use it in GitHub Desktop.
Watch that _.sortBy() method!

It turns out that underscoreJS's .sortBy() method is case sensitive, so ordering arrays may not work the way you expect it if you have entities with different casing.

// Things  like 'myUser1' and 'MyUser2' will show out of order within a a given list, if using sortBy the following way:
users = _.sortBy(users, 'name');

//The proper way to do a case 'insensitive' sorting using underscore would be:
users = _.sortBy(users, function (user) {
	return user.name.toLowerCase();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment