为什么javascript的排序功能不能很好地工作?

发布于 2024-11-09 06:52:15 字数 267 浏览 0 评论 0原文

这个简单的 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]);

产生结果: 11.17、2.73、3.12 而不是 2.73、3.12、11.17

这是为什么?我该如何解决它?

提前致谢!

This simple 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.12 instead of 2.73, 3.12, 11.17.

Why is that and how can I fix it?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梓梦 2024-11-16 06:52:15

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

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]);
}

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]);
}
痴情换悲伤 2024-11-16 06:52:15

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

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

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

x.sort(function(a, b) { return a > b ? 1 : -1});
永不分离 2024-11-16 06:52:15

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

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

此 customSort() 函数将以升序对数组 in_place 进行排序。

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.

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.

江城子 2024-11-16 06:52:15

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

排序没有按照您想要的方式进行,因为默认排序是词法排序(即数组元素转换为字符串并按字母顺序进行比较)。您可以为 sort() 提供自己的比较函数:

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

其次,for...in 实际上并没有告诉您有关数组是否排序正确的具体信息,因为枚举的 for...in 未定义(尽管大多数但并非所有浏览器都广泛执行您所期望的操作)。使用 for 循环代替(实际上,对于数组通常应该如此):

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

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():

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

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

for (var i = 0, len = x.length; i < len; ++i) {
    alert(x[i]);
}
酒中人 2024-11-16 06:52:15

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

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

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

You are not iterating properly. It should be:

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

When you use for..in in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文