# Underscore: sortBy() based on multiple attributes

## Underscore: sortBy() based on multiple attributes

I am trying to sort an array with objects based on multiple attributes. I.e if the first attribute is the same between two objects a second attribute should be used to comapare the two objects. For example, consider the following array:
var patients = [
[{name: ‘John’, roomNumber: 1, bedNumber: 1}],
[{name: ‘Lisa’, roomNumber: 1, bedNumber: 2}],
[{name: ‘Chris’, roomNumber: 2, bedNumber: 1}],
[{name: ‘Omar’, roomNumber: 3, bedNumber: 1}]
];

Sorting these by the roomNumber attribute i would use the following code:
var sortedArray = _.sortBy(patients, function(patient) {
return patient[0].roomNumber;
});

This works fine, but how do i proceed so that ‘John’ and ‘Lisa’ will be sorted properly?

### Solution 1:

`sortBy` says that it is a stable sort algorithm so you should be able to sort by your second property first, then sort again by your first property, like this:

``````var sortedArray = _(patients).chain().sortBy(function(patient) {
return patient[0].name;
}).sortBy(function(patient) {
return patient[0].roomNumber;
}).value();
``````

When the second `sortBy` finds that John and Lisa have the same room number it will keep them in the order it found them, which the first `sortBy` set to “Lisa, John”.

### Solution 2:

Here’s a hacky trick I sometimes use in these cases: combine the properties in such a way that the result will be sortable:

``````var sortedArray = _.sortBy(patients, function(patient) {
return [patient[0].roomNumber, patient[0].name].join("_");
});
``````

However, as I said, that’s pretty hacky. To do this properly you’d probably want to actually use the core JavaScript `sort` method:

``````patients.sort(function(x, y) {
var roomX = x[0].roomNumber;
var roomY = y[0].roomNumber;
if (roomX !== roomY) {
return compare(roomX, roomY);
}
return compare(x[0].name, y[0].name);
});

// General comparison function for convenience
function compare(x, y) {
if (x === y) {
return 0;
}
return x > y ? 1 : -1;
}
``````

Of course, this will sort your array in place. If you want a sorted copy (like `_.sortBy` would give you), clone the array first:

``````function sortOutOfPlace(sequence, sorter) {
var copy = _.clone(sequence);
copy.sort(sorter);
return copy;
}
``````

Out of boredom, I just wrote a general solution (to sort by any arbitrary number of keys) for this as well: have a look.

### Solution 3:

I know I’m late to the party, but I wanted to add this for those in need of a clean-er and quick-er solution that those already suggested. You can chain sortBy calls in order of least important property to most important property. In the code below I create a new array of patients sorted by Name within RoomNumber from the original array called patients.

``````var sortedPatients = _.chain(patients)
.sortBy('Name')
.sortBy('RoomNumber')
.value();
``````

### Solution 4:

btw your initializer for patients is a bit weird, isn’t it?
why don’t you initialize this variable as this -as a true array of objects-you can do it using _.flatten() and not as an array of arrays of single object, maybe it’s typo issue):

``````var patients = [
{name: 'Omar', roomNumber: 3, bedNumber: 1},
{name: 'John', roomNumber: 1, bedNumber: 1},
{name: 'Chris', roomNumber: 2, bedNumber: 1},
{name: 'Lisa', roomNumber: 1, bedNumber: 2},
{name: 'Kiko', roomNumber: 1, bedNumber: 2}
];
``````

I sorted the list differently and add Kiko into Lisa’s bed; just for fun and see what changes would be done…

``````var sorted = _(patients).sortBy(
function(patient){
return [patient.roomNumber, patient.bedNumber, patient.name];
});
``````

inspect sorted and you’ll see this

``````[
{bedNumber: 1, name: "John", roomNumber: 1},
{bedNumber: 2, name: "Kiko", roomNumber: 1},
{bedNumber: 2, name: "Lisa", roomNumber: 1},
{bedNumber: 1, name: "Chris", roomNumber: 2},
{bedNumber: 1, name: "Omar", roomNumber: 3}
]
``````

so my answer is : use an array in your callback function
this is quite similar to Dan Tao‘s answer, I just forget the join (maybe because I removed the array of arrays of unique item :))
Using your data structure, then it would be :

``````var sorted = _(patients).chain()
.flatten()
.sortBy( function(patient){
return [patient.roomNumber,
patient.bedNumber,
patient.name];
})
.value();
``````

and a testload would be interesting…

### Solution 5:

None of these answers are ideal as a general purpose method for using multiple fields in a sort. All of the approaches above are inefficient as they either require sorting the array multiple times (which, on a large enough list could slow things down a lot) or they generate huge amounts of garbage objects that the VM will need to cleanup (and ultimately slowing the program down).

Here’s a solution that is fast, efficient, easily allows reverse sorting, and can be used with `underscore` or `lodash`, or directly with `Array.sort`

The most important part is the `compositeComparator` method, which takes an array of comparator functions and returns a new composite comparator function.

``````/**
* Chains a comparator function to another comparator
* and returns the result of the first comparator, unless
* the first comparator returns 0, in which case the
* result of the second comparator is used.
*/
function makeChainedComparator(first, next) {
return function(a, b) {
var result = first(a, b);
if (result !== 0) return result;
return next(a, b);
}
}

/**
* Given an array of comparators, returns a new comparator with
* descending priority such that
* the next comparator will only be used if the precending on returned
* 0 (ie, found the two objects to be equal)
*
* Allows multiple sorts to be used simply. For example,
* sort by column a, then sort by column b, then sort by column c
*/
function compositeComparator(comparators) {
return comparators.reduceRight(function(memo, comparator) {
return makeChainedComparator(comparator, memo);
});
}
``````

You’ll also need a comparator function for comparing the fields you wish to sort on. The `naturalSort` function will create a comparator given a particular field. Writing a comparator for reverse sorting is trivial too.

``````function naturalSort(field) {
return function(a, b) {
var c1 = a[field];
var c2 = b[field];
if (c1 > c2) return 1;
if (c1 < c2) return -1;
return 0;
}
}
``````

(All the code so far is reusable and could be kept in utility module, for example)

Next, you need to create the composite comparator. For our example, it would look like this:

``````var cmp = compositeComparator([naturalSort('roomNumber'), naturalSort('name')]);
``````

This will sort by room number, followed by name. Adding additional sort criteria is trivial and does not affect the performance of the sort.

``````var patients = [
{name: 'John', roomNumber: 3, bedNumber: 1},
{name: 'Omar', roomNumber: 2, bedNumber: 1},
{name: 'Lisa', roomNumber: 2, bedNumber: 2},
{name: 'Chris', roomNumber: 1, bedNumber: 1},
];

// Sort using the composite
patients.sort(cmp);

console.log(patients);
``````

Returns the following

``````[ { name: 'Chris', roomNumber: 1, bedNumber: 1 },
{ name: 'Lisa', roomNumber: 2, bedNumber: 2 },
{ name: 'Omar', roomNumber: 2, bedNumber: 1 },
{ name: 'John', roomNumber: 3, bedNumber: 1 } ]
``````

The reason I prefer this method is that it allows fast sorting on an arbitrary number of fields, does not generate a lot of garbage or perform string concatenation inside the sort and can easily be used so that some columns are reverse sorted while order columns use natural sort.

### Solution 6:

Simple Example from http://janetriley.net/2014/12/sort-on-multiple-keys-with-underscores-sortby.html (courtesy of @MikeDevenney)

Code

``````var FullySortedArray = _.sortBy(( _.sortBy(array, 'second')), 'first');
``````

``````var FullySortedArray = _.sortBy(( _.sortBy(patients, 'roomNumber')), 'name');