Last active
December 14, 2015 14:19
-
-
Save codeslinger/5100109 to your computer and use it in GitHub Desktop.
x.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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