解释为什么的Array.sort这样的行为 [英] Explain why Array.sort Act this way

查看:115
本文介绍了解释为什么的Array.sort这样的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们试图排序数字数组人说用这个

when we try to sort an array of Numbers people say use this

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
   debugger;
   return (a > b) ? (1) : (-1);
});
debugger;
print(numbers);

如果我们看参数A,B在调试模式中,我们看到下面的

if we watched the parameter a,b in the debugger mode we see the following

4,2
4,5
1,3
5,1
2,1
2,3
4,3

我需要知道或排序功能从内工作如何给我这些参数的解释?或者我需要发生了什么有人解释一下吗?

i need to know or an explanation of how sort function works from inside to give me these parameter ??? or i need some one explain whats happening ?

推荐答案

我用这样的:

var num = [4,2,5,1,3];
num.sort(function(a,b){return a-b;});

这篇关于解释为什么的Array.sort这样的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆