[英]Sort multidimensional array in JavaScript
I need to sort an array containing arrays in ascending numerical order. 我需要按数字升序对包含数组的数组进行排序。 The data structure looks like this
数据结构如下所示
array = [[escalation],//integer
[name],
[email],
[blackberry]];
I try to sort the array using this function (sorting by escalation) 我尝试使用此函数对数组进行排序(按升级排序)
function sortfcn(a,b){
if(a[0]<b[0]){
return -1;
}
else if(a[0]>b[0]){
return 1;
}
else{
return 0;
}
}
But my output still looks incorrect... 但我的输出看起来仍然不正确......
0 0 10 12 14 16 18 20 20 8 0 0 10 12 14 16 18 20 20 8
Any advice on how to fix this? 关于如何解决这个问题的任何建议?
From the sort output you provided, it looks like JavaScript is reading the array elements as strings. 从您提供的排序输出中,看起来JavaScript正在将数组元素作为字符串读取。 See if parseInt works:
看看parseInt是否有效:
function sortfcn(a,b){
if(parseInt(a[0])<parseInt(b[0])){
return -1;
}
else if(parseInt(a[0])>parseInt(b[0])){
return 1;
}
else{
return 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.