Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given an array of objects:

{
    key: "a",
    value: 42
},
{
    key: "d",
    value: 28
},
{
    key: "c",
    value: 92
},
{
    key: "b",
    value: 87
}

and an array of keys:

["c", "a", "b", "d"]

Is there a ECMAScript function or a 3rd-party JavaScript library that lets you sort - in one line/function call - the first array of objects, to match the order of the keys specified in the second array, such that the result is:

{
    key: "c",
    value: 92
},
{
    key: "a",
    value: 42
},
{
    key: "b",
    value: 87
},
{
    key: "d",
    value: 28
}

Other questions that provide a function or algorithm:

Similar/related questions:

share|improve this question
    
Have you tried using a combination of array.sort(function()) and insertion sort/selection sort? or you just need a third party solution? –  neoeahit Sep 17 '13 at 20:27
    
I found a couple of other questions with solutions after posting this, and updated the question. So now the goal is to find something that provides it out of the box, rather than re-implementing it. –  mkopala Sep 17 '13 at 20:30
    
Is there a ECMAScript function or a 3rd-party JavaScript library that lets you... is a shopping question and will be closed as such. –  Mathletics Sep 17 '13 at 20:32
    
I am baffled as to why this question is being voted for closure. –  jessegavin Sep 17 '13 at 20:32
    
if there were a native way to do it, someone would have mentioned it in one of the other questions. –  lincolnk Sep 17 '13 at 20:37

2 Answers 2

up vote 4 down vote accepted

Just use indexOf to convert the key to the correct order:

var order = ["c", "a", "b", "d"];
_.sortBy(arr, function(obj){ 
    return _.indexOf(order, obj.key);
});

Fiddle

If there are a lot of keys, then it would be advantageous to make a hash-map out of the array, like:

var order = ["c", "a", "b", "d"];
var orderMap = {};
_.each(order, function(i) { orderMap[i] = _.indexOf(order, i); });

This makes the key-sorting lookup constant time rather than O(n). (Fiddle)

share|improve this answer
    
Awesome answer. So simple. –  jessegavin Sep 17 '13 at 20:31
1  
I like this. It would still be nice to find it in an existing library is a single function. I'm using CoffeeScript, so this actually makes a nice one-liner: arr = _.sortBy arr, (obj) -> _.indexOf order, obj.key. –  mkopala Sep 17 '13 at 20:56
2  
@mkopala you can add your own functions to underscore using _.mixin() underscorejs.org/#mixin –  jessegavin Sep 23 '13 at 19:36

I can't claim that this is the most efficient way, but you can use the key for each object as a key for properties in another object. Then simply access them by these keys.

for (x = 0; x < objn.length; x++) {
    newobj[objn[x].key] = objn[x];
}
objn = [];
for (x = 0; x < keys.length; x++) {
    objn.push(newobj[keys[x]]);
}
console.log(objn);

http://jsfiddle.net/WdehF/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.