Here's what I mean. Suppose I have an array of objects like
var objs = [ { foo: 5, bar: "something"},
{ foo: 4912, bar: "blah" },
{ foo: -12, bar: "hehe" } ];
and an array that defines an ordering on bar
values, like
var arr = ["blah", "something", "hehe"]
Does JavaScript have a good way of getting a version of objs
to
[ { foo: 4912, bar: "blah" },
{ foo: 5, bar: "something"}
{ foo: -12, bar: "hehe" } ];
???
The best way I know is like
objs.sort(function(x,y){
var ix = arr.indexOf(x),
iy = arr.indexOf(y);
if(ix<iy) return -1;
else if(ix==iy) return 0;
else return 1;
});
but I'm wondering if there's a way that is more compact.