简体   繁体   English

javascript array.sort不适用于整数

[英]javascript array.sort not working with integers

I'm having issues with javascript array sorting and integers. 我在javascript数组排序和整数方面遇到问题。 I have these functions defined: 我定义了以下功能:

function sortA(a,b){ 
  return a - b;
}
function sortD(a,b){ 
  return b - a;
}

then in a jquery $.each, I put some content in the arrays: 然后在jquery $ .each中,将一些内容放入数组中:

$('.'+sort_column).each(function(index, value) {

    var current_num = $.trim($(this).text())

    current_num = parseInt(current_num, 10); //convert to integer for numeric sorting.

    valuesArr.push(current_num);  

    console.log(typeof index); //just checking...this shows number in the console
});

var sort_asc = valuesArr.sort(sortA);
var sort_desc = valuesArr.sort(sortD);
console.log(sort_asc);
console.log(sort_desc);

but in the console, I get the arrays in the same order. 但是在控制台中,我以相同的顺序获取数组。

//console
[1214500, 1214499, 1214497, 1214481, 1214432, 1214421, 1214419, 1214418, 1214369, 1214045, 1205691]
[1214500, 1214499, 1214497, 1214481, 1214432, 1214421, 1214419, 1214418, 1214369, 1214045, 1205691]

curiously, if I append a string to the end, the sorting works 奇怪的是,如果我在字符串末尾附加一个字符串,则排序有效

console.log( valuesArr.sort(sortD)  + "asdf");
console.log( valuesArr.sort(sortA)  + "asdf");

//console
[1214500,1214499,1214497,1214481,1214432,1214421,1214419,1214418,1214369,1214045,1205691asdf] 
[1205691,1214045,1214369,1214418,1214419,1214421,1214432,1214481,1214497,1214499,1214500asdf]

I don't know why I even tried that, but there you go. 我不知道为什么我什至尝试过,但是你去了。 This is the first time I've worked with this method, so I've likely missed something quite basic. 这是我第一次使用这种方法,因此我很可能错过了一些基本的知识。 Many thanks for any help! 非常感谢您的帮助!

You're sorting the same array's instance, that's why the order is the same. 您正在对同一数组的实例进行排序,这就是顺序相同的原因。 Basically sort changes the content of the array itself. 基本上排序会更改数组本身的内容。

So both of them will be in desc order (that is the last sorting you're doing). 因此,它们都将按降序排列(这是您正在执行的最后排序)。 If you want to keep the original instance intact, you should create new arrays: 如果要保持原始实例不变,则应创建新的数组:

var sort_asc = valuesArr.slice(0).sort(sortA);
var sort_desc = valuesArr.slice(0).sort(sortD);

console.log(valuesArr);
console.log(sort_asc);
console.log(sort_desc);

See also slice . 另请参见slice

.sort() [MDN] sorts the array in-place . .sort() [MDN]对数组进行原位排序。 Both variables sort_asc and sort_desc reference the same array and therefore the output is the same. 变量sort_ascsort_desc引用相同的数组,因此输出是相同的。

you can simply change your sortA() and sortD() like following: 您可以像下面这样简单地更改您的sortA()sortD()

function sortA(arr) {
    return arr.sort(function(a, b) {
       return a - b;
    });
}

function sortD(arr) {
    return arr.sort(function(a, b) {
       return b - a;
    });
}

And then use like following: 然后使用如下所示:

var myarr = [1214500, 1214499, 1214497, 1214481, 1214432, 1214421, 
             1214419, 1214418, 1214369, 1214045, 1205691];

sortA(myarr);

sortD(myArr);

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM