排序多维数组javascript [英] sort multidimensional array javascript

查看:68
本文介绍了排序多维数组javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对一个双精度的多维数组进行排序.

I would like to sort a multidimensional array of doubles.

数组看起来像这样:[[1,2],[2,3],[5,6],[8,9]]

The array looks like this : [[1,2],[2,3],[5,6],[8,9]]

我想按X值对它进行排序,并保持x,y值成对.

I would like to sort it by the X value, and keep the x,y values paired.

我在网站上搜索了多维排序,并找到了这些这样的线程,其中排序功能是修改如下:

I searched the site for multidimentional sorting and found threads like these where the sort function is modified like so:

location.sort(function(a,b) {

  // assuming distance is always a valid integer
  return parseInt(a.distance,10) - parseInt(b.distance,10);

});

但是,我不确定如何修改此功能以使其对我有用.

I'm not sure how to modify this function to work for me, however.

推荐答案

只需比较数组值-

var myarray =  [[1,2],[2,3],[5,6],[8,9]];

myarray.sort(function(a,b) { return a[0] - b[0]; });

这篇关于排序多维数组javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆