基于另一个数组排序包含对象的数组[重复]

时间:2022-09-25 08:54:17

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

可能重复:JavaScript - 基于另一个整数数组对数组进行排序Javascript - 基于另一个数组排序数组

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'
}]

2 个解决方案

#1


2  

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/

通常的小提琴:http://jsfiddle.net/joplomacedo/haqFH/

#2


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:

我们可以使用sort()函数来执行此操作,方法是将自定义函数传递给它进行比较。给定a或b进行比较时,此函数必须返回3个可能的值:

return -1 if a is indexed lower than b

如果a索引低于b,则返回-1

return 0 if a is considered equal to b

如果a被认为等于b,则返回0

return 1 if a is indexed greater than b

如果a的索引大于b,则返回1

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.

此函数将接收您在数组中定义的对象,并查找该值在arr数组中的位置,即您要比较的数组。然后它比较索引,并根据需要返回值。

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

我们通过将函数传递给sort()函数来使用此函数:

testArray.sort(sortFunction)

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

其中testArray是您尝试排序的数组。

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/

你可以看看这里,我做了这个例子,你可以在调用sort函数之前和之后看到数组中的第二个对象被“警告”。 http://jsfiddle.net/Sqys7/

#1


2  

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/

通常的小提琴:http://jsfiddle.net/joplomacedo/haqFH/

#2


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:

我们可以使用sort()函数来执行此操作,方法是将自定义函数传递给它进行比较。给定a或b进行比较时,此函数必须返回3个可能的值:

return -1 if a is indexed lower than b

如果a索引低于b,则返回-1

return 0 if a is considered equal to b

如果a被认为等于b,则返回0

return 1 if a is indexed greater than b

如果a的索引大于b,则返回1

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.

此函数将接收您在数组中定义的对象,并查找该值在arr数组中的位置,即您要比较的数组。然后它比较索引,并根据需要返回值。

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

我们通过将函数传递给sort()函数来使用此函数:

testArray.sort(sortFunction)

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

其中testArray是您尝试排序的数组。

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/

你可以看看这里,我做了这个例子,你可以在调用sort函数之前和之后看到数组中的第二个对象被“警告”。 http://jsfiddle.net/Sqys7/