sort函数如何在JavaScript中工作,以及compare函数 [英] How does sort function work in JavaScript, along with compare function

查看:103
本文介绍了sort函数如何在JavaScript中工作,以及compare函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如已经提到的那样:排序功能如何在JavaScript中运行,以及比较功能?
如果我有一个数组,并且我做了 array.sort(比较)现在书中写道如果比较函数返回 ab (数组的两个索引)然后根据结果是否大于0,小于0或相等的事实工作到0.但是,它究竟是如何工作的?我无法解决这个问题。

As already asked: how does sort function work in JavaScript, along with the compare function? If I have an array, and I do array.sort(compare) now it was written in the book that if the compare function returns a-b (two indices of the array) then it works based on the fact that whether the result is greater than 0, less than 0 or equal to 0. But, how exactly does it work? I could not work it out.

推荐答案

compare函数必须带两个参数,通常称为 a b 。然后根据这些值 a b 使比较函数返回0,大于0或小于0。

The "compare" function must take two arguments, often referred to as a and b. Then you make the compare function return 0, greater than 0, or less than 0, based on these values, a and b.


  1. 如果 a 大于 b ,则返回大于0

  2. 返回0如果 a 等于 b

  3. 如果 a 小于 b <,则返回小于0

  1. Return greater than 0 if a is greater than b
  2. Return 0 if a equals b
  3. Return less than 0 if a is less than b

使用这三个返回值,并且只有两个参数,可以编写一个可以对任何类型进行排序的比较函数输入数据类型或复杂数据结构。

With these three return values, and only two arguments, it is possible to write a compare function that can sort any type of input data type, or complex data structures.

然后,当您使用自定义比较函数调用sort()时,将在对中调用compare函数-be-sorted list,以确定正确的排序。

Then, when you call sort(), with your custom compare function, the compare function is called on pairs in your to-be-sorted list, to determine the proper ordering.

让我们来看一个简单的例子......假设你只是在排序一些数字,所以我们有一个非常好的排序简单比较函数:

Lets walk through a simple example... Suppose you're only sorting some numbers, so we have a very simple compare function:

function compare(a,b) {
    return a - b;
}

如果a大于0,只需从a中减去b将始终返回大于零b,如果它们相等则为0;如果a小于b,则小于零。所以它符合比较函数的要求。

Simply subtracting b from a will always return greater than zero if a is larger than b, 0 if they are equal, or less than zero if a is less than b. So it meets the requirements for a compare function.

现在让我们假设这是我们要排序的数字列表:

Now lets suppose this is our list of numbers to sort:

var numbers = [1,5,3.14];

当你致电 numbers.sort(比较),在内部实际执行:

When you call numbers.sort(compare), internally it will actually execute:

compare(1,5);     // Returns -4, a is less than b
compare(1,3.14);  // Return -2.14, a is less than b
compare(5,3.14);  // returns 1.86, a is greater than b

如果您曾进行过手动排序或按字母顺序排序,你完成了同样的事情,可能没有意识到。即使您可能需要比较几十个或几百个项目,但您一次只能比较两个数字(或作者的姓氏或其他)。再次查看三个数字或短名单,首先要比较前两个数字:

If you've ever done manual sorting or alphabetizing, you've done precisely the same thing, probably without realizing it. Even though you may have dozens or hundreds of items to compare, you're constantly comparing only two numbers (or author's last names, or whatever) at a time. Going through or short list of three numbers again, you'd start by comparing the first two numbers:


  1. 1大于或小于5?小于,所以把这两个数字放在我们的列表中:1,5

  2. 3.14是大于还是小于1?大于,所以它在新列表中的1之后

  3. 在我们的新列表中3.14是大于还是小于5?不到,所以它在5之前。我们的新名单现在是[1,3.14,5]

因为你可以提供自己的compare()函数,可以对任意复杂的数据进行排序,而不仅仅是数字。

Because you can provide your own compare() function, it is possible to sort arbitrarily complex data, not just numbers.

这篇关于sort函数如何在JavaScript中工作,以及compare函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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