我想排序第一个数组并拥有第二个数组,第三个数组与第一个数组相同。如果我选择排序第二个,另外两个将再次跟随。正如你在下面看到的,我需要它来处理字符串和数字。任何关于良好算法的建议都将受到赞赏。最好使用push()方法。
let array_one = ["bravo_one", "charlie_two", "alpha_three", "echo_four", "delta_five"]
let array_two = [1, 2, 3, 4, 5]
let array_three = ["golf_one", "zulu_two", "tango_three", "november_four", "kilo_five"]
// Some Logic expected here
let new_one = array_one.sort(function(a, b){
return (a.localeCompare(b));
})
let new_two = []
let new_three = []
// Exprected Output
console.log(new_one); // ["alpha_three", "bravo_one", "charlie_two", "delta_five", "echo_four"]
console.log(new_two); // [3, 1, 2, 5, 4]
console.log(new_three); // ["tango_three", "golf_one", "zulu_two", "kilo_five", "november_four"]
// Some Logic expected here
let new_one = []
let new_two = array_two.sort(function(a, b){
return b.length - a.length;
})
let new_three = []
// Exprected Output
console.log(new_one); // ["bravo_one", "charlie_two", "alpha_three", "echo_four", "delta_five"]
console.log(new_two); // [1, 2, 3, 4, 5]
console.log(new_three); // ["golf_one", "zulu_two", "tango_three", "november_four", "kilo_five"]
// Some Logic expected here
let new_one = []
let new_two = []
let new_three = array_three.sort(function(a, b){
return (a.localeCompare(b));
})
// Exprected Output
console.log(new_one); // ["bravo_one", "delta_five", "echo_four", "tango_three", "charlie_two"]
console.log(new_two); // [1, 5, 4, 3, 2]
console.log(new_three); // ["golf_one", "kilo_five", "november_four", "tango_three", "zulu_two"]
答案 0 :(得分:2)
array_one
,array_two
和array_three
看起来应该是一个对象。特别是如果array_one[0]
,array_two[0]
和array_three[0]
包含相关数据。
首先更改获取数据的方式,或者:
var bigarray = array_one.map(function(_,i) {
return {
name: array_one[i],
number: array_two[i],
extra: array_three[i]
}
);
现在您可以排序:
bigarray.sort(function(a,b) {
return a.name.localeCompare(b.name);
});
按你的意愿迭代。
答案 1 :(得分:1)
我认为您应该以不同的方式设置数据结构。为了便于实施。 新结构使工作更轻松
var items = [
{name: 'bravo_two', age: 2, last_name: 'zulu_two'},
{name: 'alpha_one', age: 1, last_name: 'tango_one'},
{name: 'charlie_three', age: 3, last_name: 'golf_three'},
{name: 'echo_five', age: 5, last_name: 'kilo_five'},
{name: 'delta_four', age: 4, last_name: 'november_four'}
];
function comp(a, b){
return a.name > b.name ? 1: -1
}
items.sort(comp);
// OR
// items.sort((a, b) => { return a.name > b.name ? 1: -1 });
for(i=0; i<items.length; i++ ){
console.log(items[i]);
}
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
如果无法更改数据结构。以下是一个实现。
var new_one = ["bravo_two", "alpha_one", "charlie_three", "echo_five", "delta_four"];
var new_two = [2, 1, 3, 5, 4];
var new_three = ["zulu_two", "tango_one", "golf_three", "kilo_five", "november_four"];
var i;
var items = [];
for(i=0;i<new_one.length;i++){
items.push(
{ name: new_one[i], age: new_two[i], last_name: new_three[i] }
);
}
// OR
// var items = new_one.map((_,i)=>{ return { name: new_one[i], age: new_two[i], extra: new_three[i] } });
function comp(a, b){
return a.name > b.name ? 1: -1
}
items.sort(comp);
// OR
// items.sort((a, b) => { return a.name > b.name ? 1: -1 });
for(i=0; i<items.length; i++ ){
console.log(items[i]);
}
new_one = [];
new_two = [];
new_three = [];
for(i=0;i<items.length;i++){
new_one.push(items[i].name);
new_two.push(items[i].age);
new_three.push(items[i].last_name);
}
console.log(new_one);
console.log(new_two);
console.log(new_three);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
答案 2 :(得分:0)
您希望首先对第一个数组进行排序,然后根据“one”,“two”等索引对进行排序,以便对第2个和第3个数组进行排序
其他答案假设第2和第3个数组都已按w.r.t排序。第一个数组。下面的回答假定第二和第三个数组可以按自己的顺序排列,并且您希望它们按“one”,“two”等新索引进行排序。
首先将“one”,“two”等映射到数字
var map = { "one" : 1, "two": 2, "three" : 3, "four" : 4, "five" : 5 };
对第一个数组进行排序并获取最新的索引
array_one.sort( ( a, b ) => a.localeCompare(b) );
获取新索引以创建第二个数组
array_two = array_one.map( s => map[s.split("_")[1]] );
使用相同的逻辑来获得第3个数组
array_three.sort( function(a,b){
var numA = map[a.split( "_" )[1]];
var numB = map[b.split( "_" )[1]];
return array_two.indexOf(numA) - array_two.indexOf(numB);
});
<强>演示强>
let array_one = ["bravo_one", "charlie_two", "alpha_three", "echo_four", "delta_five"];
let array_two = [1, 2, 3, 4, 5];
let array_three = ["golf_one", "zulu_two", "tango_three", "november_four", "kilo_five"];
var map = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5
};
array_one.sort((a, b) => a.localeCompare(b));
array_two = array_one.map(s => map[s.split("_")[1]]);
array_three.sort( function(a,b){
var numA = map[a.split( "_" )[1]];
var numB = map[b.split( "_" )[1]];
return array_two.indexOf(numA) - array_two.indexOf(numB);
});
console.log(array_one, array_two,array_three);
答案 3 :(得分:0)
您可以使用您的列表创建一个矩阵,排序一个并通过引用更改其他矩阵:
// Change position between two items
Array.prototype.swap = function(a,b,u) {
this.moved = this.moved || [];
if(u && this.moved.indexOf(a) != -1 && this.moved.indexOf(b) != -1) return false;
this.tmp = this[a];
this[a] = this[b];
this[b] = this.tmp;
this.moved.push(a);
this.moved.push(b);
};
// Change order of all arrays by reference
function orderby(matriz,line){
line = line || 0;
var moded = [];
var oders = matriz[line].slice(0,matriz[line].lenght);
matriz[line].sort();
for(var j = 0; j < oders.length; j++){
k = matriz[line].indexOf(oders[j])
for(var n = 0; n < matriz.length; n++){
n != line && matriz[n].swap(j,k,1);
}
}
}
// Array X Array = Matriz
var matriz = [
["alpha_one", "bravo_two", "charlie_three", "delta_four", "echo_five"], //line 0
[1, 2, 3, 4, 5], //line 1
["tango_one", "zulu_two", "golf_three", "november_four", "kilo_five"] //line 2
]
var line = 2; //We will order all lines by reference line 2
orderby(matriz,line);
console.log(matriz[0]);
console.log(matriz[1]);
console.log(matriz[2]);
&#13;
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
&#13;