Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对数组进行排序,然后获取索引并使用索引移动所有对应的元素?_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript 如何对数组进行排序,然后获取索引并使用索引移动所有对应的元素?

Javascript 如何对数组进行排序,然后获取索引并使用索引移动所有对应的元素?,javascript,arrays,sorting,Javascript,Arrays,Sorting,我有5个具有相同索引的不同数组,例如: person[0]="john", address[0]= "Druid Valley", city[0]="Atlanta", amount[0]=2000, need[0]=100; person[1]="emily", address[1]="50 decatur", city[1]="Chicago", amount[1]=300; need[1]=50; 我需要按照need[]数组的降序对所有数组重新排序,然后根据need[I]的新索引重新排

我有5个具有相同索引的不同数组,例如:

person[0]="john", address[0]= "Druid Valley", city[0]="Atlanta", amount[0]=2000, need[0]=100; 
person[1]="emily", address[1]="50 decatur", city[1]="Chicago", amount[1]=300; need[1]=50;
我需要按照need[]数组的降序对所有数组重新排序,然后根据need[I]的新索引重新排列其他数组的顺序。我正在使用javascript

谢谢,


John

need
数组进行排序并保存排序排列,然后将该排列应用于其他数组。具体如何做到这一点取决于您使用的语言。例如,Matlab
sort
函数可以返回排序数组和排序排列。

不排序“需要”。创建一个索引数组,然后根据需要对该数组进行排序。但是,您没有指定语言,因此得到了JavaScript:

var person = [], need = [];

var person = ["E", "B", "A", "C", "D"];
var need = [111, 444, 555, 333, 222];

var index = [];
var i = person.length;
while (i--) {
  index.push(i);
}
var comparator = function(a, b) {
  var need_a = need[a];
  var need_b = need[b];

  // For robustness - non-numbers get sorted last:
  if (typeof need_a != 'number' || isNaN(need_a)) need_a = -Infinity;
  if (typeof need_b != 'number' || isNaN(need_b)) need_b = -Infinity;

  if (need_a < need_b) return 1;
  if (need_b < need_a) return -1;
  return 0;
}
index.sort(comparator);

// at this point, person[index[0]] is the person with the biggest need.

var sorted_person = [];
var i = index.length;
while (i--) {
  sorted_person[i] = person[index[i]];
}

// at this point, sorted_person[0] is the person with the biggest need.

console.log(sorted_person);
var person=[],need=[];
var person=[“E”、“B”、“A”、“C”、“D”];
var need=[111444555333222];
var指数=[];
var i=人长;
而(我--){
指数推(i);
}
var比较器=功能(a,b){
var need_a=需要[a];
var need_b=need[b];
//为确保稳健性-非数字最后排序:
if(typeof need_a!=“number”| isNaN(need_a))need_a=-无穷大;
if(typeof need_b!=“number”| isNaN(need_b))need_b=-无穷大;
if(need_a
制作一个副本数组,该数组将具有与所需数组相同的值(称为排序数组)。
然后查看原始的need数组-对于每个单元格,找到它在排序数组中的位置,并将其他数组中的匹配值与它们在排序数组中出现的单元格编号相同

谢谢你,Amadan,这是Javascript语言(我应该从一开始就指定)-我如何使用它同时对所有数组进行重新排序,而不仅仅是个人?我需要一种可以同时对所有元素重新排序的方法(在元素之间循环)。您可能不需要-只需将原始数组与
索引一起使用,就像我在最后一行中显示的那样。如果您确实需要它们作为排序数组,那么创建新的数组会更容易:
var i=index.length;变量排序的人=[],排序的地址=[];虽然(i--){sorted_person[i]=person[index[i]];sorted_address[i]=address[index[i]];}
我尝试了这个方法,但排序不起作用,我得到的是未排序的值。呃,在这里起作用。我将更新代码以显示添加的位,并将数组替换为更容易看到结果的数组。谢谢你,Amadan,你的代码与你编写的代码一致。我注意到的问题是,在大约800个元素中,可能有20-50个元素具有“NaN”(这些元素取自基于web的表单,parseFloat不起作用,因为它们当时可能是空字段)。我应该做些什么更改以允许这些需要的值[]?您可以粘贴代码来实现您所说的吗?我是一个初学者,实用的细节帮助我理解。非常感谢。