我在这上面花了很长时间,但无法在这里发现这个错误。
var test_arr = [['79',0],['8',1],['30', 2],['580',3],['59',4]]
本质上,我有一个从函数生成的类型的多元数组。它的行为不像普通数组,因为我的 sort fxnvar sort_arrays = function(a,b) {return a[0]-b[0]};
无法正确排序数组值。
我试图确保我有一个实际的数组,并且我没有调用替代排序方法。
因此:
console.log(arr_test.sort(sort_arrays));
给出:[[8,1],[30,2],[59,4],[79,0],[580,3]]
但是,在我的 fxn 上调用它,
console.log(input_kde.sort(sort_arrays));
,给出:[...,[580, 0.002],[59,0.001]]
在尝试生成一些可打印的输出时,我调用.toString
了我的结果,并得到了正确排序的输出,这只会加深我的困惑。
我尝试过显式类型转换parseFloat(a[0])
但无济于事。
我还尝试确认这确实是一个数组:
console.log(typeof(input_kde)); //#> object
console.log(input_kde.constructor); //#> function Array() { [native code] }
console.log(input_kde instanceof Array); //#> true
此外, input_kde[0] 是一个数组, input_kde[0][0] 是一个数字
console.log(input_kde[0] instanceof Array); //#> true
console.log(typeof(input_kde[0][0])); //#> number
我错过了什么?
编辑2
var input_kde = [[22,0.0017095221275204712],[24,0.001712291003699343],[28,0.001716821328044571],[307,0.0009792081392809542],[33,0.0017205981968323918],[34,0.0017211029300063302],[341,0.0010085669284820244],[35,0.0017215244128879188],[387,0.0010558490092862406],[4,0.0016696065108796395],[40,0.0017223891946762664],[448,0.0010667514507313397],[491,0.0010124434821844265],[519,0.0009480280002363708],[580,0.0007465298453339767],[59,0.0017073128314605394]]