Javascript多维数组按数字顺序排序 [英] Javascript multidimentional array sorting by numerical order
本文介绍了Javascript多维数组按数字顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个二维数组,需要对它进行数字排序.这是数组的示例:
I have a two dimensional array that I need to sort numerically. Here is a sample of the array:
[0] [1]
3 320
55B 250
26 100
55A 260
56 310
89 420
我需要按[0]值进行数字排序.值存储为字符串.我已经弄清楚了如何按字母和数字顺序对数组进行排序,但是由于偶尔会出现A和B,因此我无法弄清楚如何对数组进行排序.
I need to order numerically by the [0] values. The values are stored as strings. I have figured how to sort arrays alphabetically and numerically but I can't figure out how to order this array because of the occasional A and B.
推荐答案
parseInt方法将忽略数字之后的任何字符串字符,从而删除A和B.
The parseInt method will ignore any string characters after the number, removing the A's and B's.
arr.sort(function(rowA, rowB){
var a = parseInt(rowA[0], 10);
var b = parseInt(rowB[0], 10);
if (a > b)
return 1;
else if (a < b)
return -1;
return 0;
});
这篇关于Javascript多维数组按数字顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文