最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

function - What really happens in Javascript Sort - Stack Overflow

programmeradmin3浏览0评论

I've seen this sort function working fine:

var arr = [1,5,3,7,8,6,4,3,2,3,3,4,5,56,7,8,8];


console.log(arr.sort(
    function(a,b) {
        return a - b;
    }
    ));

But I don't really understand the mechanics of this little function. When it is paring a and b, which numbers of the array is it really paring? If say, it picked up the first two numbers 1 and 5, the function will return -4. What does that mean to the sort order? Or is it just the negative boolean value? Even if it is, how does the sort really happen?

I've seen this sort function working fine:

var arr = [1,5,3,7,8,6,4,3,2,3,3,4,5,56,7,8,8];


console.log(arr.sort(
    function(a,b) {
        return a - b;
    }
    ));

But I don't really understand the mechanics of this little function. When it is paring a and b, which numbers of the array is it really paring? If say, it picked up the first two numbers 1 and 5, the function will return -4. What does that mean to the sort order? Or is it just the negative boolean value? Even if it is, how does the sort really happen?

Share Improve this question edited Oct 15, 2019 at 10:08 Amulya K Murthy 1301 gold badge2 silver badges15 bronze badges asked Dec 21, 2011 at 11:14 Adnan KhanAdnan Khan 6652 gold badges8 silver badges20 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

Basically, the sort works by paring two elements at a time. A parison is more than a boolean--you have three options: less than, equal and greater than. In JavaScript, these three values are represented by n < 0, 0 and n > 0 respectively.

In other words, negative numbers mean a < b; 0 means a = b and positive means a > b.

To answer the broader question: there are some relatively fast algorithms for sorting a list by paring its elements. The most popular is Quicksort; however, Quicksort is not stable so some engines (Firefox's for sure) use a different algorithm. A simple stable sort is Mergesort.

Sorting algorithms are often some of the first algorithms analyzed in intro CS classes because they are simple but still interesting and nontrivial enough to illustrate how to analyze algorithms in general. You should read about them for this reason, and simply because they're pretty cool.

Slightly random aside:

You could also imagine using a special type (like an enum) for this sort of thing. The paring function could return LT, GT or EQ as appropriate, for example. However, in a dynamic language like JavaScript, it's much easier just to use numbers. In languages more obsessed with types (like Haskell :)), using a special order type makes more sense.

You got 3 options minus (-), equal (==) and plus (+):

  • minus: if a - b < 0 then a before b
  • equals: doesn't matter
  • plus: if a - b > 0 then b before a

See this thread for more information: Javascript Array.sort implementation?

When passing a function to Array.sort, it will be used to pare a and b. How they will be sorted depends on what the function returns:

  • If result < 0, then a es before b;
  • If result == 0, then a and b are already in the right order;
  • If result > 0, then a es after b.

If sort is called without an argument, the items are converted to strings and pared alfabetically.

By the way, which algorithm sort uses depends entirely on the browser's implementation. It could be quick sort, merge sort, or anything else. The ECMAScript standard doesn't specify any requirements in this regard.

发布评论

评论列表(0)

  1. 暂无评论