6

Possible Duplicate:
JavaScript - Sort an array based on another array of integers
Javascript - sort array based on another array

If I have an array like this:

['one','four','two']

And another array like this:

[{
  key: 'one'
},{
  key: 'two'
},{
  key: 'four'
}]

How would I sort the second array so it’s key property follows the order of the first? In this case, I want:

[{
  key: 'one'
},{
  key: 'four'
},{
  key: 'two'
}]
Community
  • 1
  • 1
David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • 2
    possible duplicate of [JavaScript - Sort an array based on another array of integers](http://stackoverflow.com/questions/4046967/javascript-sort-an-array-based-on-another-array-of-integers) and/or [Javascript - sort array based on another array](http://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array?lq=1) – Ted Hopp Nov 22 '12 at 18:33
  • @TedHopp not quite, that solves two flat arrays but this cases uses object keys wich makes it more difficult to sort effectively (unless you want to search the second for each iteration). – David Hellsing Nov 22 '12 at 18:36
  • I expected something like this to work, but somehow... it doesn't. :-/ `a2.sort(function(a,b) { a1[a2.indexOf(a)] - a1[a2.indexOf(b)] })` – Frank van Puffelen Nov 22 '12 at 18:59

2 Answers2

6

We can use the sort() function to do this by passing it a custom function which does the comparison. This function has to return 3 possible values given a or b to compare:

return -1 if a is indexed lower than b

return 0 if a is considered equal to b

return 1 if a is indexed greater than b

With this in mind, we can define a function such as this:

function sortFunction(a,b){
    var indexA = arr.indexOf(a['key']);
    var indexB = arr.indexOf(b['key']);
    if(indexA < indexB) {
        return -1;
    }else if(indexA > indexB) {
        return 1;
    }else{
        return 0;       
    }
}

This function will take in the objects you defined in your array, and find where that value is in the arr array, which is the array you're comparing to. It then compares the index, and returns the values as needed.

We use this function by passing the function into the sort() function as such:

testArray.sort(sortFunction)

where testArray is the array you're trying to sort.

You can take a look at here, where I did this example, and you can see the second object in your array being "alerted" to you, before and after the sort function was called. http://jsfiddle.net/Sqys7/

MysticXG
  • 1,437
  • 10
  • 10
4

Here's my take on it:

function orderArray(array_with_order, array_to_order) {
    var ordered_array = [], 
        len = array_to_order.length,
        len_copy = len,
        index, current;

    for (; len--;) {
        current = array_to_order[len];
        index = array_with_order.indexOf(current.key);
        ordered_array[index] = current;
    }

    //change the array
    Array.prototype.splice.apply(array_to_order, [0, len_copy].concat(ordered_array));
}

Sample implementation:

var array_with_order = ['one', 'four', 'two'],

    array_to_order = [
        {key: 'one'},
        {key: 'two'},
        {key: 'four'}
    ];

orderArray(array_with_order, array_to_order);

console.log(array_to_order); //logs [{key: 'one'}, {key: 'four'}, {key: 'two'}];

The usual fiddle: http://jsfiddle.net/joplomacedo/haqFH/

João Paulo Macedo
  • 15,297
  • 4
  • 31
  • 41