javascript 根据另一个整数数组对数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4046967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort an array based on another array of integers
提问by Sarathi Hansen
Let's say I have an array: [0,3,4,2,5,1].
比方说,我有一个数组:[0,3,4,2,5,1]。
What I want to do is sort an array such as:
我想要做的是对数组进行排序,例如:
["one", "two", "three", "four", "five", "six"]
So that the order corresponds to the first array.
使顺序对应于第一个数组。
This would be the output:
这将是输出:
["one", "four", "five", "three", "six", "two"]
Is there an easy way to accomplish this?
有没有简单的方法来实现这一点?
回答by Nick Craver
You can do something like this:
你可以这样做:
function getSorted(arr, sortArr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
console.log(sortArr[i], arr[i]);
result[i] = arr[sortArr[i]];
}
return result;
}
var arr = ["one", "two", "three", "four", "five", "six"];
var sortArr = [0, 3, 4, 2, 5, 1];
alert(getSorted(arr, sortArr));
Note:this assumes the arrays you pass in are equivalent in size, you'd need to add some additional checks if this may not be the case.
注意:这假设您传入的数组大小相等,如果情况并非如此,您需要添加一些额外的检查。
回答by user982671
I was asked this on a phone interview. Then do it without creating another array, supposing the array is very large. I don't know if this is the answer since I wasn't able to do on the call (damn!), but here's what I came up with.
我在电话采访中被问到这个问题。然后在不创建另一个数组的情况下执行此操作,假设数组非常大。我不知道这是否是答案,因为我无法在通话中进行(该死!),但这是我想出的。
var my_obj_array = ['a', 'b', 'c', 'd'];
var my_indicies = [3, 1, 0, 2];
// desired result ['d', 'b', 'a', 'c']
var temp = {};
for (var i = 0; i < my_indicies.length; i++) {
temp[i] = my_obj_array[i]; // preserve
var j = my_indicies[i];
if (j in temp) {
my_obj_array[i] = temp[j];
delete temp[j];
} else {
my_obj_array[i] = my_obj_array[j];
}
}
回答by kennebec
orderedArray= function(arr,order){
return order.map(function(itm){return arr[itm]});
}
var sequence= [0, 3, 4, 2, 5, 1],arr=["one","two","three","four","five","six"]
arr=new orderedArray(arr,sequence);
/* returned value: (Array)
one,four,five,three,six,two
*/
//You can make the order an unindexed property of the array, // and call array.ordered()
//您可以使订单成为数组的无索引属性, // 并调用array.ordered()
Array.prototype.ordered= function(order){
var arr= this;
order=order || this.order;
return order.map(function(itm){
return arr[itm];
});
}
var arr= ["one","two","three","four","five","six"],
sequence= [0, 3, 4, 2, 5, 1];
arr.order=sequence;
arr.ordered()
/* returned value: (Array)
one,four,five,three,six,two
*/
回答by Mic
Not sur how you get your first array, but you could use an array of objects instead of [0,3,4,2,5,1]:
不是你如何获得你的第一个数组,但你可以使用一个对象数组而不是[0,3,4,2,5,1]:
var arr = [
{n:0, s:'one'},
{n:3, s:'four'},
{n:4, s:'five'},
{n:2, s:'three'},
{n:5, s:'six'},
{n:1, s:'two'}
]
And avoid to process it.
并避免对其进行处理。
回答by Belal mazlom
You can use first array as directory for sorting second one using map method:
您可以使用第一个数组作为目录,使用 map 方法对第二个数组进行排序:
const firstArray = [0, 3, 4, 2, 5, 1];
const secondArray = ['one', 'two', 'three', 'four', 'five', 'six'];
const result = firstArray.map((item) => {
return secondArray[item];
});
// result = ["one", "four", "five", "three", "six", "two"]
回答by d.b
let inds = [0,3,4,2,5,1];
let x = ["one", "two", "three", "four", "five", "six"];
let x2 = []; //Output
inds.forEach(ind => x2.push(x[ind]));
x2
//(6)?["one", "four", "five", "three", "six", "two"]
回答by kamla
class test1
{
public static String[] sort(int[] array,String[] str)
{
String[] out=new String[str.length];
for(int i=0;i<str.length;i++)
{
out[i]=str[array[i]];
}
return out;
}
}

