基于来自不同长度的另一个数组的值对数组进行排序

时间:2021-10-02 20:14:53

I've looked through several posts with similar with issues but I couldn't find one which solves my problem. The others all seemed to be sorting using another array of the same size or by value.

我已经浏览了几个与问题类似的帖子,但我找不到解决我问题的帖子。其他人似乎都在使用相同大小或值的另一个数组进行排序。

I have two arrays which look like this:

我有两个看起来像这样的数组:

var allCategories = ['Campus', 'Building', 'Floor', 'Room', 'Lecture Theatre', 'Lab'];
var currentCategories = ['Room', 'Lab', 'Campus'];

How can I sort currentCategories so that the order matches that of allCategories?

如何对currentCategories进行排序以使订单与allCategories的顺序相匹配?

Desired output:

期望的输出:

currentCategories --> ['Campus', 'Room', 'Lab'];

3 个解决方案

#1


5  

"Sort this array by the indices of its elements in that array":

“按照该数组中元素的索引对此数组进行排序”:

currentCategories.sort(function(a, b) {
  return allCategories.indexOf(a) - allCategories.indexOf(b);
});
// => ["Campus", "Room", "Lab"]

#2


3  

If all that you want is the order of allCategories with the members of currentCategories, you can do the following.

如果您想要的只是具有currentCategories成员的allCategories的顺序,则可以执行以下操作。

allCategories.filter(function(x){return currentCategories.indexOf(x) != -1})

This assumes that you are treating each array as a set of non-repeating elements. As mentioned in the comments, this method may drop duplicate elements from the final value, or not order duplicate elements in the way you might intend.

这假设您将每个数组视为一组非重复元素。正如评论中所提到的,此方法可能会从最终值中删除重复元素,或者不按照您可能想要的方式对重复元素进行排序。

#3


0  

The Array.Sort method can be supplied a custom function. You could do something like this:

可以为Array.Sort方法提供自定义函数。你可以这样做:

currentCategories.sort(function(a, b) {
    var i = allCategories.indexOf(a),
        j = allCategories.indexOf(b);           
    return i - j;
});

I haven't checked the behaviour for when there are values in currentCategories that are not in allCategories. But then, it wouldn't be living up to its name if that were the case.

我没有检查当currentCategories中的值不在allCategories中时的行为。但是,如果是这样的话,就不会辜负它的名字。

If this is a common case, you could generalise it along the following lines:

如果这是一种常见情况,您可以按以下方式对其进行概括:

function sortByList(list) {
    return function (a, b) { return list.indexOf(a) - list.indexOf(b); };
}

And call it thusly:

并称之为:

currentCategories.sort(sortByList(allCategories));

#1


5  

"Sort this array by the indices of its elements in that array":

“按照该数组中元素的索引对此数组进行排序”:

currentCategories.sort(function(a, b) {
  return allCategories.indexOf(a) - allCategories.indexOf(b);
});
// => ["Campus", "Room", "Lab"]

#2


3  

If all that you want is the order of allCategories with the members of currentCategories, you can do the following.

如果您想要的只是具有currentCategories成员的allCategories的顺序,则可以执行以下操作。

allCategories.filter(function(x){return currentCategories.indexOf(x) != -1})

This assumes that you are treating each array as a set of non-repeating elements. As mentioned in the comments, this method may drop duplicate elements from the final value, or not order duplicate elements in the way you might intend.

这假设您将每个数组视为一组非重复元素。正如评论中所提到的,此方法可能会从最终值中删除重复元素,或者不按照您可能想要的方式对重复元素进行排序。

#3


0  

The Array.Sort method can be supplied a custom function. You could do something like this:

可以为Array.Sort方法提供自定义函数。你可以这样做:

currentCategories.sort(function(a, b) {
    var i = allCategories.indexOf(a),
        j = allCategories.indexOf(b);           
    return i - j;
});

I haven't checked the behaviour for when there are values in currentCategories that are not in allCategories. But then, it wouldn't be living up to its name if that were the case.

我没有检查当currentCategories中的值不在allCategories中时的行为。但是,如果是这样的话,就不会辜负它的名字。

If this is a common case, you could generalise it along the following lines:

如果这是一种常见情况,您可以按以下方式对其进行概括:

function sortByList(list) {
    return function (a, b) { return list.indexOf(a) - list.indexOf(b); };
}

And call it thusly:

并称之为:

currentCategories.sort(sortByList(allCategories));