javascript 为什么javascript的排序功能不好用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6093874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:25:29  来源:igfitidea点击:

Why doesn't the sort function of javascript work well?

javascriptarrayssorting

提问by Christos Mitsis

This simple javascript

这个简单的javascript

var x = new Array();
x[0] = 2.73;
x[1] = 11.17;
x[2] = 3.12
x.sort();

for(var i in x)
    alert(x[i]);

produces the results: 11.17, 2.73, 3.12instead of 2.73, 3.12, 11.17.

产生结果: 11.17, 2.73, 3.12而不是2.73, 3.12, 11.17.

Why is that and how can I fix it?

为什么会这样,我该如何解决?

Thanks in advance!

提前致谢!

回答by Tom

It's sorting alphabetically, try passing your own sorting function:

它按字母顺序排序,尝试传递您自己的排序函数:

var x = new Array();
x[0] = 2.73;
x[1] = 11.17;
x[2] = 3.12;

numberSort = function (a,b) {
    return a - b;
};

x.sort(numberSort);

for(var i in x) {
    alert(x[i]);
}

回答by sje397

By default, Array.sortwill sort alphabetically (lexographically)...but you can supply your own function. Try:

默认情况下,Array.sort将按字母顺序(按字典顺序)排序...但您可以提供自己的函数。尝试:

x.sort(function(a, b) { return a > b ? 1 : -1});

回答by Tim Down

Between them, the existing answers tell you everything, but none of them mention both of the problems in your code. Here's the full answer:

在它们之间,现有的答案会告诉您一切,但它们都没有提到您代码中的这两个问题。这是完整的答案:

The sort isn't doing what you want because the default sort is lexical (i.e. the array elements are converted to strings and compared alphabetically). You can provide your own comparison function to sort():

排序不符合您的要求,因为默认排序是词法排序(即数组元素转换为字符串并按字母顺序进行比较)。您可以提供自己的比较功能sort()

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

Secondly, for...inis actually telling you nothing concrete about whether your array is sorted correctly, because the enumeration of for...inis not defined (even though most but not all browsers do broadly what you'd expect). Use a forloop instead (as indeed you generally should for arrays):

其次,for...in实际上并没有告诉您有关数组是否正确排序的任何具体信息,因为for...in未定义的枚举(即使大多数但并非所有浏览器都大致按照您的期望执行)。使用for循环代替(实际上您通常应该为数组使用):

for (var i = 0, len = x.length; i < len; ++i) {
    alert(x[i]);
}

回答by Roshan Raju

Array.sort() function treats its elements as Strings and if no function is passed to the sort() statement, it converts the elements to Unicode and sorts. Therefore, it is advised to pass a custom sort Function whenever sorting numbers.

Array.sort() 函数将其元素视为字符串,如果没有函数传递给 sort() 语句,它将元素转换为 Unicode 并进行排序。因此,建议在对数字进行排序时传递自定义排序函数。

function customSort(a, b){
     return a - b; 
}
console.log([-11,-2, 0 ,100].sort(customSort));

This customSort() function will sort the array in_place in ascending order.

这个 customSort() 函数将按升序对数组 in_place 进行排序。

回答by Darin Dimitrov

You are not iterating properly. It should be:

您没有正确迭代。它应该是:

for (var i = 0; i < x.length; i++) {
    alert(x[i]);
}

When you use for..inin javascript this will loop through the properties of the object and the order of iteration is undefined. You should be seeing some strange output as well such as all the functions defined in the Array class.

当您for..in在 javascript 中使用时,这将遍历对象的属性,并且迭代顺序未定义。您还应该看到一些奇怪的输出,例如 Array 类中定义的所有函数。