Sort on multiple keys with Underscore's sortBy()
The Underscore library's sortBy() function allows us to sort a collection of objects by a single key name. The enhancement request to sort on multiple fields hasn't been implemented yet.
What to do in the meantime? You could call sort() with your own comparison method, or switch to another helper library like Lodash. That can be more trouble than it's worth.
A quick and dirty alternative is to call sortBy() multiple times in order of the least important field to the most important. This takes advantage of sortBy()'s stable sort algorithm. "Stable sort" means when two items are equal the sorter will leave them in the same order it found them. If we call sortBy multiple times, each pass will nudge it toward its final order. Bucket sort works on the same principle.
Say we want to sort our list by name, then age. To use sortBy, we'll call it in order of the least important key - age - and then the more important key, name.
Here it is step by step.
Given our list:
Sort by the least important key:
Sort again on the more important key:
... which is now sorted by name, then age.
This isn't recommended for sorting large lists, since you're calling sort multiple times. The overhead is minimal with a short list.