Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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/1/ssh/2.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:在特定数组结构上使用array.sort()_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript:在特定数组结构上使用array.sort()

Javascript:在特定数组结构上使用array.sort(),javascript,arrays,sorting,Javascript,Arrays,Sorting,对于任何现有索引I,我有一个如下结构的数组: 例如,其内容可以如下所示: myArray[0] = [2, 35] myArray[1] = [3, 57] myArray[2] = [5, 12] myArray[3] = [6, 42] 我想以升序对这个数组进行排序,同时保持相同的结构,并使用aValue作为排序参数。在上面的示例中,对于结果排序数组的每个索引,我将得到以下结果: 索引0:[5,12] 索引1:[2,35] 索引2:[6,42] 索引2:[3,57] 我真的不明白如何使

对于任何现有索引I,我有一个如下结构的数组:

例如,其内容可以如下所示:

myArray[0] = [2, 35]
myArray[1] = [3, 57]
myArray[2] = [5, 12]
myArray[3] = [6, 42]
我想以升序对这个数组进行排序,同时保持相同的结构,并使用aValue作为排序参数。在上面的示例中,对于结果排序数组的每个索引,我将得到以下结果:

  • 索引0:[5,12]
  • 索引1:[2,35]
  • 索引2:[6,42]
  • 索引2:[3,57]
我真的不明白如何使用array.sort()实现这一点,或者如果可能的话。感谢您这样使用
比较功能

var myArray=[];
myArray[0]=[2,35];
myArray[1]=[3,57];
myArray[2]=[5,12];
myArray[3]=[6,42];
myArray=myArray.sort(函数(x,y){
返回x[1]-y[1];
});
log(myArray)
myArray[0] = [2, 35]
myArray[1] = [3, 57]
myArray[2] = [5, 12]
myArray[3] = [6, 42]