Problem: I have a large array of objects that I want to be able to easily sort based on the order of another array. For example: I have an array of objects that look like:
[...{ key: "a", value: "hi" }, { key: "b", value: "bye" }...]
And I want sort the array by another array based on a given parameter like key
. I can do this by calling a simple function that return my large source array in sorted order.
The function:
function sortArrayByArray(arrayToSort, arrayToSortBy, keyToSortBy) {
let reference_object = {};
// Build a reference object
for (let i = 0; i < arrayToSortBy.length; i++) {
reference_object[arrayToSortBy[i][keyToSortBy]] = i;
}
arrayToSort.sort(function(a, b) {
return reference_object[a[keyToSortBy]] - reference_object[b[keyToSortBy]];
});
return arrayToSort;
}
Let’s break this down. The sortArrayByArray() function receives 3 arguments, the arrayToSort
which is our large source array, arrayToSortby
which tells our sorting function how we want things ordered, and finally keyToSortBy
which is the parameter that we want to use for sorting.
The first thing we do is create a simple object that will hold reference information later used by the sorting function, we call the object reference_object
.
let reference_object = {};
Next, we need to populate the reference_object
with information that will be helpful when we start sorting.
for (let i = 0; i < arrayToSortBy.length; i++) {
reference_object[arrayToSortBy[i][keyToSortBy]] = i;
}
Simply, what we’re doing is looping over the arrayToSortBy
and assigning an integer, i
, to the matching key value. For example, in our arrayToSortBy
the first object is { key: "b" }
, in our reference_object this would become { b: 0 } (remember, we start on the zero index). If our full arrraToSortBy
looks like:
const arrayToSortBy = [{ key: "b" }, { key: "c" }, { key: "d" }, { key: "a" }];
Our new reference_object would look like:
{
a: 3,
b: 0,
c: 1,
d: 2
}
Finally, we use the sort() function to ‘sort’ each of the objects in the arrayToSort
based on it’s matching “key’s” value. Sort takes in two values and determines which is greater using a comparison function and returns a -1, 0, 1 value. Because we first create the reference_object to define the order which we want to sort things we aren’t leaving it up to the sort() function to make that decision for us based on the string value of a certain parameter or something similar.
All together:
const arrayToSortBy = [{ key: "b" }, { key: "c" }, { key: "d" }, { key: "a" }];
const arrayToSort = [
{ key: "a", value: "hi" },
{ key: "a", value: "hi" },
{ key: "b", value: "hi" },
{ key: "b", value: "hi" },
{ key: "c", value: "hi" },
{ key: "b", value: "hi" },
...
]
function sortArrayByArray(arrayToSort, arrayToSortBy, keyToSortBy) {
let reference_object = {};
for (var i = 0; i < arrayToSortBy.length; i++) {
reference_object[arrayToSortBy[i][keyToSortBy]] = i;
}
arrayToSort.sort(function(a, b) {
return reference_object[a[keyToSortBy]] - reference_object[b[keyToSortBy]];
});
return arrayToSort;
}
console.log(sortArrayByArray(arraySource, arrayReference, "key"));