jashkenas/underscore — pull request #1751 by megawac (2014-07-16 17:12:15+00:00)
Title: sortBy comparitor compares arrays

Another try at getting `sortBy` support for multiple criteria since #1359 was rejected. I'd argue this approach is more robust as its implementation is based on the return of the iterator instead of only handling multi prop lookups. Thus we can implement the same behaviour as #1359 with a `properties` mixin if desired later.

```
_.mixin({
properties: function(props) {
return function(obj) {
return _.map(props, function(prop) {
return obj[prop];
});
}
}
});

_.sortBy([{a: 1, b: 2}, {a: 1, b: 3}, {a: 1}, {a: 2, b: -1}, {b: 5}], _.properties(["a", "b"]));
```

```
_.sortBy([{a: 1, b: 2}, {a: 1, b: -1}, {a: 1, b: 3}, {a: 2, b: -5}], function(x) {
return [x.a, x.b];
});
// => [{"a": 1,"b": -1},{"a": 1,"b": 2},{"a": 1,"b": 3},{"a": 2,"b": -5}]
```

The thing that tripped me up implementing this is that the comparitor ranks `undefined > anything`. I'd argue that `undefined < anything` but that's just me.

I'd really like to see this behaviour in v2.