Skip to content

Instantly share code, notes, and snippets.

@boukeversteegh
Last active February 15, 2023 09:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boukeversteegh/3219ffb912ac6ef7282b1f5ce7a379ad to your computer and use it in GitHub Desktop.
Save boukeversteegh/3219ffb912ac6ef7282b1f5ce7a379ad to your computer and use it in GitHub Desktop.
Sorting multiple arrays in Javascript
/**
* Sorts all arrays together with the first. Pass either a list of arrays, or a map. Any key is accepted.
* Array|Object arrays [sortableArray, ...otherArrays]; {sortableArray: [], secondaryArray: [], ...}
* Function comparator(?,?) -> int optional compareFunction, compatible with Array.sort(compareFunction)
*/
function sortArrays(arrays, comparator = (a, b) => (a < b) ? -1 : (a > b) ? 1 : 0) {
let arrayKeys = Object.keys(arrays);
let sortableArray = Object.values(arrays)[0];
let indexes = Object.keys(sortableArray);
let sortedIndexes = indexes.sort((a, b) => comparator(sortableArray[a], sortableArray[b]));
let sortByIndexes = (array, sortedIndexes) => sortedIndexes.map(sortedIndex => array[sortedIndex]);
if (Array.isArray(arrays)) {
return arrayKeys.map(arrayIndex => sortByIndexes(arrays[arrayIndex], sortedIndexes));
} else {
let sortedArrays = {};
arrayKeys.forEach((arrayKey) => {
sortedArrays[arrayKey] = sortByIndexes(arrays[arrayKey], sortedIndexes);
});
return sortedArrays;
}
}
let people = ["john", "benny", "sally", "george"];
let peopleIds = [10, 20, 30, 40];
sortArrays([people, peopleIds]);
[["benny", "george", "john", "sally"], [20, 40, 10, 30]] // output
sortArrays({people, peopleIds});
{"people": ["benny", "george", "john", "sally"], "peopleIds": [20, 40, 10, 30]} // output
@milahu
Copy link

milahu commented Dec 17, 2020

function sortArrays is a bad name .. how about parallelSort?

default comparator is simply (a, b) => (b - a) for ascending order
sort only looks for the sign (plus or minus), no need for +1 or -1

typeof arrays should be array, so sortableArray is always arrays[0]
dont rely on ordered map values .. when accepting objects-of-arrays, require the object-key to get sortableArray

let sortedIndexes = indexes.sort( .. sort mutates in-place AND returns, so sortedIndexes == indexes

.map( is slower than for (let i = 0; ....) { .. especially in the hot-code function sortByIndexes

variable names are too similar, only adding s is hard to read, add List for example

.. other than that, good job : )

@boukeversteegh
Copy link
Author

Thank you for taking the time to find and report everything wrong with this piece code.
I hope it gave you some relief.

@milahu
Copy link

milahu commented Dec 17, 2020

I hope it gave you some relief.

the best therapy! even better than publishing a better version : P

@boukeversteegh
Copy link
Author

😂

@rocketsarefast
Copy link

This is awesome. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment