Skip to content

Instantly share code, notes, and snippets.

@codeslinger
Last active December 14, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeslinger/5100109 to your computer and use it in GitHub Desktop.
Save codeslinger/5100109 to your computer and use it in GitHub Desktop.
x.js
/*
* items -- array of objects with a 'location' field
* userPrefs -- array of user-specified location labels, in user-specified order
*/
function sortByLocation(items, userPrefs) {
return sortBy(items, userPrefs, 'location');
}
function sortBy(items, userPrefs, column) {
items.sort(function(a,b) {
var aIdx = userPrefs.indexOf(a[column]),
bIdx = userPrefs.indexOf(b[column]);
if (aIdx == -1) {
aIdx = 1000000;
}
if (bIdx == -1) {
bIdx = 1000000;
}
return (aIdx - bIdx);
});
return items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment