[英]Sort an array in the same order of another array
I have a few arrays of 50+ names like this.我有几个像这样的 50 多个名称的数组。
["dan", "ryan", "bob", "steven", "corbin"]
["bob", "dan", "steven", "corbin"]
I have another array that has the correct order.我有另一个具有正确顺序的数组。 Note that the second array above does not include all of the names, but I still want it to follow the order of the following:请注意,上面的第二个数组不包含所有名称,但我仍然希望它遵循以下顺序:
["ryan", "corbin", "dan", "steven", "bob"]
There is no logical order to it, they are just in this order.它没有逻辑顺序,它们只是按照这个顺序。 What makes sense to me is to compare each array against the correctly ordered one.对我来说有意义的是将每个数组与正确排序的数组进行比较。 I think I saw some people doing this with PHP, but I was not able to find a JavaScript solution.我想我看到有些人用 PHP 做这件事,但我找不到 JavaScript 解决方案。 Does anyone have any idea how to do this?有谁知道如何做到这一点? I've been trying for a few hours and I'm stumped.我已经尝试了几个小时,但被难住了。
Use indexOf()
to get the position of each element in the reference array, and use that in your comparison function.使用indexOf()
获取每个元素在引用数组中的位置,并在您的比较函数中使用它。
var reference_array = ["ryan", "corbin", "dan", "steven", "bob"]; var array = ["bob", "dan", "steven", "corbin"]; array.sort(function(a, b) { return reference_array.indexOf(a) - reference_array.indexOf(b); }); console.log(array); // ["corbin", "dan", "steven", "bob"]
Searching the reference array every time will be inefficient for large arrays.对于大型数组,每次搜索参考数组将是低效的。 If this is a problem, you can convert it into an object that maps names to positions:如果这是一个问题,您可以将其转换为将名称映射到位置的对象:
var reference_array = ["ryan", "corbin", "dan", "steven", "bob"]; reference_object = {}; for (var i = 0; i < reference_array.length; i++) { reference_object[reference_array[i]] = i; } var array = ["bob", "dan", "steven", "corbin"]; array.sort(function(a, b) { return reference_object[a] - reference_object[b]; }); console.log(array);
You can realize some sorter by patter factory function.您可以通过模式工厂功能实现某种排序器。 Then create sorter using your pattern and apply it to your arrays:然后使用您的模式创建排序器并将其应用于您的数组:
function sorterByPattern(pattern) {
var hash = {};
pattern.forEach(function(name, index) { hash[name] = index });
return function(n1, n2) {
if (!(n1 in hash)) return 1; // checks if name is not in the pattern
if (!(n2 in hash)) return -1; // if true - place that names to the end
return hash[n1] - hash[n2];
}
}
var sorter = sorterByPattern(["ryan", "corbin", "dan", "steven", "bob"]);
var arrays = [
["dan", "ryan", "bob", "steven", "corbin"],
["bob", "dan", "steven", "corbin"]
/* ... */
];
arrays.forEach(function(array) { array.sort(sorter) });
I had the same issue now and I tried a bit different approach.我现在遇到了同样的问题,我尝试了一些不同的方法。 It does not sort the arrays but filtering the order-list according to sorting-lists so it has got some limitations but for my needs it's event better because it gets rid of incorrect values from sorted list:它不会对数组进行排序,而是根据排序列表过滤订单列表,因此它有一些限制,但对于我的需要,它更好,因为它从排序列表中删除了不正确的值:
function sortOrder(getOrder,getArr){ return getOrder.filter(function(order){ return getArr.some(function(list){ return order === list; }); }); } //arrays var order = ["ryan", "corbin", "dan", "steven", "bob"]; var arA = ["dan", "ryan", "bob", "steven", "corbin"]; var arB = ["bob", "dan", "steven", "corbin"]; var arC = ["bob","ryan"]; var arD = ["bob","bob","corbin"]; //remove repetition var arE = ["unrecognizedItem","corbin","steven","ryan"]; //remove the item not included in order array //print results document.body.innerHTML = sortOrder(order,arA)+'<br/>'; document.body.innerHTML += sortOrder(order,arB)+'<br/>'; document.body.innerHTML += sortOrder(order,arC)+'<br/>'; document.body.innerHTML += sortOrder(order,arD)+'<br/>'; document.body.innerHTML += sortOrder(order,arE)+'<br/>';
If you need to place values of your array in the recurring order, meaning:如果您需要按重复顺序放置数组的值,这意味着:
Input: [1, 2, 4, 4, 3, 3, 2, 1]输入:[1, 2, 4, 4, 3, 3, 2, 1]
Output: [1, 2, 3, 4, 1, 2, 3, 4]输出:[1, 2, 3, 4, 1, 2, 3, 4]
Then you can use 2 functions below.然后你可以使用下面的2个功能。 The first one uses the second one.第一个使用第二个。
For the first function you will need to provide 2 arguments:对于第一个函数,您需要提供 2 个参数:
1st argument: your array of items that should be ordered ( Input from above)第一个参数:您应该订购的项目数组(从上面输入)
2nd argument: array of the correct order ( [1, 2, 3, 4] for the example above)第二个参数:正确顺序的数组( [1, 2, 3, 4]上面的例子)
function sortByOrder (array, order) {
const arrayOfArrays = order.map(v => {
return [...Array(howMany(v, array))].map(undef => v);
});
const tempArray = [];
arrayOfArrays.forEach((subArr, i) => {
let index = order.indexOf(order[i]);
subArr.forEach(duplicate => {
tempArray[index] = duplicate;
index += order.length;
});
});
return tempArray.filter(v => v);
}
function howMany(value, array) {
const regExp = new RegExp(value, 'g');
return (array.join(' ').match(regExp) || []).length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.