Skip to content

Instantly share code, notes, and snippets.

@xuyang2
Created March 20, 2014 15:19
Show Gist options
  • Save xuyang2/9666156 to your computer and use it in GitHub Desktop.
Save xuyang2/9666156 to your computer and use it in GitHub Desktop.
var _ = require('underscore');
function sortByWeight (arr, weightMap) {
arr.sort(function (e1, e2) {
var w1 = weightMap[e1];
var w2 = weightMap[e2];
if (_.isNumber(w1) && _.isNumber(w2)) {
return w1 - w2;
} else {
return 0;
}
});
}
function sortByFixedOrder (arr, order) {
var weightMap = {};
for (var i = 0; i < order.length; i++) {
var o = order[i];
weightMap[o] = i;
};
sortByWeight(arr, weightMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment