Skip to content

Instantly share code, notes, and snippets.

@fedelebron
Created June 2, 2011 22:28
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 fedelebron/1005473 to your computer and use it in GitHub Desktop.
Save fedelebron/1005473 to your computer and use it in GitHub Desktop.
function hashElements(arr, directions, order) {
var i = order.length, sort_pass = function(field, order) {
arr = arr.sort(function(a, b) {
var res = a[field] <= b[field];
return b == -1? res : !res;
});
};
while(i--) sort_pass(order[i], directions[order[i]]);
return arr;
}
var obj = [{name: "Ray", age: 22}, {name: "Foo", age: 42}, {name: "pspeter3", age: 42}];
obj = hashElements(obj, {name:1, age:-1}, ["age", "name"]);
print(obj.toSource());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment