How does Javascript's sort() work?

北城以北 提交于 2019-11-26 01:35:45

问题


How does the following code sort this array to be in numerical order?

var array=[25, 8, 7, 41]

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

I know that if the result of the computation is...

Less than 0: \"a\" is sorted to be a lower index than \"b\".
Zero: \"a\" and \"b\" are considered equal, and no sorting is performed.
Greater than 0: \"b\" is sorted to be a lower index than \"a\".

Is the array sort callback function called many times during the course of the sort?

If so, I\'d like to know which two numbers are passed into the function each time. I assumed it first took \"25\"(a) and \"8\"(b), followed by \"7\"(a) and \"41\"(b), so:

25(a) - 8(b) = 17 (greater than zero, so sort \"b\" to be a lower index than \"a\"): 8, 25

7(a) - 41(b) = -34 (less than zero, so sort \"a\" to be a lower index than \"b\": 7, 41

How are the two sets of numbers then sorted in relation to one another?

Please help a struggling newbie!


回答1:


Is the array sort callback function called many times during the course of the sort?

Yes

If so, I'd like to know which two numbers are passed into the function each time

You could find out your self with:

array.sort((a,b) => {
  console.log(`comparing ${a},${b}`);
  return a > b ? 1
               : a === b ? 0 
                         : -1;
});

EDIT

This is the output I've got:

25,8
25,7
8,7
25,41



回答2:


The JavaScript interpreter has some kind of sort algorithm implementation built into it. It calls the comparison function some number of times during the sorting operation. The number of times the comparison function gets called depends on the particular algorithm, the data to be sorted, and the order it is in prior to the sort.

Some sort algorithms perform poorly on already-sorted lists because it causes them to make far more comparisons than in the typical case. Others cope well with pre-sorted lists, but have other cases where they can be "tricked" into performing poorly.

There are many sorting algorithms in common use because no single algorithm is perfect for all purposes. The two most often used for generic sorting are Quicksort and merge sort. Quicksort is often the faster of the two, but merge sort has some nice properties that can make it a better overall choice. Merge sort is stable, while Quicksort is not. Both algorithms are parallelizable, but the way merge sort works makes a parallel implementation more efficient, all else being equal.

Your particular JavaScript interpreter may use one of those algorithms or something else entirely. The ECMAScript standard does not specify which algorithm a conforming implementation must use. It even explicitly disavows the need for stability.




回答3:


Pairs of values are compared, one pair at a time. The pairs that are compared are an implementation detail--don't assume they will be the same on every browser. The callback can be anything (so you can sort strings or Roman numerals or anything else where you can come up with a function that returns 1,0,-1).

One thing to keep in mind with JavaScript's sort is that it is not guaranteed to be stable.




回答4:


Is the array sort callback function called many times during the course of the sort?

Yes, that's exactly it. The callback is used to compare pairs of elements in the array as necessary to determine what order they should be in. That implementation of the comparison function is not atypical when dealing with a numeric sort. Details in the spec or on some other more readable sites.




回答5:


Is the array sort callback function called many times during the course of the sort?

Since this is a comparison sort, given N items, the callback function should be invoked on average (N * Lg N) times for a fast sort like Quicksort. If the algorithm used is something like Bubble Sort, then the callback function will be invoked on average (N * N) times.

The minimum number of invocations for a comparison sort is (N-1) and that is only to detect an already sorted list (i.e. early out in Bubble Sort if no swaps occur).




回答6:


run this code. You can see the exact step by step sorting process from the beginning to the end.

var array=[25, 8, 7, 41]
var count = 1;
array.sort( (a,b) => { 
console.log(`${count++}). a: ${a} | b: ${b}`);
return a-b;
});
console.log(array);



回答7:


var array=[25, 8, 7, 41]

array.sort(function(a,b){
  console.log(`a = ${a} , b = ${b}`);
  return a - b
});

OUTPUT

  • a = 8 , b = 25
  • a = 7 , b = 8
  • a = 41 , b = 7
  • a = 41 , b = 8
  • a = 41 , b = 25

in my Browser (Google Chrome Version 70.0.3538.77 (Official Build) (64-bit) ) in the first iteration, the argument a is the Second element in an array and argument b is the First element of an array.

if the Compare function returns

  1. Negative Value than b is moved forward to a.
  2. positive Value than a is moved forward to b.
  3. 0(Zero) both a & b will remain as it is.


来源:https://stackoverflow.com/questions/1494713/how-does-javascripts-sort-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!