有人可以解释下面的代码:
var values = [213, 16, 2058, 54, 10, 1965, 57, 9];
values.sort(function(value1,value2){ return value2 - value1; });
我无法理解如何从数组值加载value1和value2,以及它如何反转排序结果。
答案 0 :(得分:3)
sort函数执行以下操作:
return value2 - value1;
让它变得更加冗长,以便我们可以看到发生了什么:
var values = [213, 16, 2058, 54, 10, 1965, 57, 9];
values.sort(function(value1,value2){
console.log(value2 + ' - ' + value1 + ' = ' + (value2 - value1) + ' | (' + (value2 - value1 > 0 ? 'positive | ' + value2 + ' should be before ' + value1 : 'negative | ' + value2 + ' should be after ' + value1) + ')');
return value2 - value1;
});
输出:
16 - 213 = -197 | (negative | 16 should be after 213)
2058 - 16 = 2042 | (positive | 2058 should be before 16)
2058 - 213 = 1845 | (positive | 2058 should be before 213)
54 - 16 = 38 | (positive | 54 should be before 16)
54 - 213 = -159 | (negative | 54 should be after 213)
10 - 16 = -6 | (negative | 10 should be after 16)
1965 - 10 = 1955 | (positive | 1965 should be before 10)
1965 - 16 = 1949 | (positive | 1965 should be before 16)
1965 - 54 = 1911 | (positive | 1965 should be before 54)
1965 - 213 = 1752 | (positive | 1965 should be before 213)
1965 - 2058 = -93 | (negative | 1965 should be after 2058)
57 - 10 = 47 | (positive | 57 should be before 10)
57 - 16 = 41 | (positive | 57 should be before 16)
57 - 54 = 3 | (positive | 57 should be before 54)
57 - 213 = -156 | (negative | 57 should be after 213)
9 - 10 = -1 | (negative | 9 should be after 10)
如果您对所有值执行以下操作,您会注意到如果value2
大于value1
,则结果将始终为正,并且它将在数组 - 基本上以反向排序的数组结束。
答案 1 :(得分:1)
value1
和value2
是您创建的函数的变量。该函数被传递给sort
函数,该函数负责对数组进行排序。
sort
函数通过在元素对上调用提供的函数来计算元素应该处于什么顺序。根据结果(负数或正数),sort
函数会对它们进行排序。
在这种情况下,如果value2
大于value1
,则返回正数,这意味着它从最大到最小排序。
答案 2 :(得分:1)
Array#sort
函数重复调用您给它的回调(称为比较器)来比较数组中的两个值。它在排序数组时根据需要执行此操作。所以排序逻辑在Array#sort
,但比较逻辑在你给它的比较器函数中。
预计比较器将返回以下三个值中的一个:
0
如果两个参数相等< 0
如果第一个参数是“小于”第二个> 0
如果第一个参数是“大于”第二个 sort
调用比较器的次数以及这些调用的顺序不是由规范决定的。
您的特定比较器正在比较数字,因此非常简单:只返回value2 - value1
。这意味着如果value1
小于value2
,比较器将返回正数;如果value1
等于value2
,则比较器将返回0
;如果value1
大于value2
,则会返回-1
。因此,这个特殊的比较器按反向数字顺序排序(最大数字首先)。
答案 3 :(得分:0)
对于几乎所有的排序功能,您需要在某个时间点比较两个值,以确定哪个值更大,哪个更小。您进行了多少次比较显然取决于排序算法的效率。
您可以在javascript和许多其他语言中提供此行为。您在sort()
中提供的任何函数都称为比较器,并且预计会返回以下三个值之一:
答案 4 :(得分:0)
在JavaScript中对数组进行排序是通过方法array.sort()来完成的,这种方法可能被误解,因为它被低估了。虽然调用sort()本身只是按字典顺序(也就是字母顺序)对数组进行排序,但是一旦超出表面,天空就是极限。 按字典顺序排列数组
按字典顺序(也就是“按字母顺序”或按字典顺序)对数组进行排序很容易。只需调用array.sort()而不传入任何参数:
//Sort alphabetically and ascending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort() //Array now becomes ["Amy", "Bob", "Bully"]
请注意,订单是递增的。为了使它降序,最简单的方法是组合另一个Array方法的帮助,array.reverse():
//Sort alphabetically and descending:
var myarray=["Bob", "Bully", "Amy"]
myarray.sort()
myarray.reverse() //Array now becomes ["Bully", "Bob", "Amy"]
现在,在开始感觉舒服之前,请考虑如果我们在由数字组成的数组上调用array.sort()会发生什么:
var myarray=[7, 40, 300]
myarray.sort() //Array now becomes [300,40,7]
虽然7在数字上小于40或300,但是按字典顺序,它更大,因此7出现在排序数组的最右边。请记住,默认情况下,array.sort()按字典顺序对其元素进行排序。
就基本用法而言,你有array.sort()。但是这种方法还有很多东西比满足眼睛要多。 Array.sort()以函数引用的形式接受一个可选参数,它几乎允许您根据任何自定义条件对数组进行排序,例如以数字方式对数组进行排序或对其进行随机排序(随机化其元素的顺序)。 将函数引用传入array.sort() 如上所述,array.sort()接受函数引用形式的可选参数(让我们称之为sortfunction)。此函数的格式如下所示:
array.sort(sortfunction)
function sortfunction(a, b){
//Compare "a" and "b" in some fashion, and return -1, 0, or 1
}
当这样的函数传递给array.sort()时,数组元素将根据每对元素“a”和“b”与函数返回值之间的关系进行排序。三个可能的返回数字是:&lt; 0(小于0),0或> 0(大于0):
Less than 0: Sort "a" to be a lower index than "b" Zero: "a" and "b" should be considered equal, and no sorting performed. Greater than 0: Sort "b" to be a lower index than "a".
例如,要对数字进行数字排序和升序,函数的主体将如下所示:
function sortfunction(a, b){
return (a - b) //causes an array to be sorted numerically and ascending
}
有关此内容的更多信息。 按数字顺序对数组进行排序
要按数字顺序对数组进行排序,只需将自定义sortfunction传递给array.sort(),它返回“a”和“b”之间的差值,这两个参数间接/自动输入函数:
//Sort numerically and ascending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return a - b}) //Array now becomes [7, 8, 25, 41]
这就像它的工作方式一样,因为只要“a”小于“b”,就会返回一个负值,这会导致较小的元素总是出现在较大的元素的左边,换句话说,就是升序。 / p>
以数字方式对数组进行排序,但降序差别不大,只需要反转两个操作数“a”和“b”:
//Sort numerically and descending:
var myarray=[25, 8, 7, 41]
myarray.sort(function(a,b){return b - a}) //Array now becomes [41, 25, 8, 71]
改组(随机化)数组的顺序
为了使数组中元素的顺序随机化,我们需要的是我们的sort函数的主体返回一个随机&lt; 0,0或&gt; 0的数字,而不管“a”和“a”之间的关系“b”。以下将解决这个问题:
//Randomize the order of the array:
var myarray=[25, 8, "George", "John"]
myarray.sort(function() {return 0.5 - Math.random()}) //Array elements now scrambled
正如你所看到的,array.sort()还有很多人想到的东西。实际上,您甚至可以对包含多个原始值的数组进行排序,但对包含属性的对象进行排序。让我们看看下一步。