按值对复杂的数组数组进行排序 [英] Sort Complex Array of Arrays by value within
问题描述
我在一个对象中设置了一个 javascript 数组,该对象与本地存储一起存储.它是 Phonegap 游戏的高分,格式为 {'0':[[score,time],[score,time]}
,其中 0 是类别.我可以使用 a[1][0]
查看分数.我想先按高分排序.
I have an array of arrays in javascript set up within an object that I'm storing with local storage. It's high scores for a Phonegap game in the format {'0':[[score,time],[score,time]}
where 0 is the category. I'm able to see the scores using a[1][0]
. I want to sort with the high scores first.
var c={'0':[[100,11],[150,12]};
c.sort(function(){
x=a[0];
y=b[0];
return y-x;
}
然而,b[0] 总是给出一个未定义的错误.
However, b[0] always gives an undefined error.
我是 Javascript 的新手,这是我作为学习经验的第一个主要测试.尽管我已经查看了许多有关 stackoverflow 的示例,但仍然无法弄清楚这一点.
I'm new to Javascript and making this is my first major test as a learning experience. Though I've looked at a number of examples on stackoverflow still can't figured this one out.
推荐答案
需要声明比较器函数的参数.
You need to declare the parameters to the comparator function.
c.sort(function(){
应该是
c.sort(function(a, b){
并且您需要在数组上调用 sort
,因为我不是我"指出
and you need to call sort
on an array as "am not i am" points out so
var c={'0':[[100,11],[150,12]]};
c[0].sort(function(a, b){
var x=a[0];
var y=b[0];
return y-x;
});
使 c
处于以下状态:
{
"0": [
[150, 12],
[100, 11]
]
}
这篇关于按值对复杂的数组数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!