How does javascript sort () work?

How does the following code sort this array in order of numbers?

var array=[25, 8, 7, 41] array.sort(function(a,b){ return a - b }) 

I know that if the result of the calculation ...

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

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

If so, I would like to know which two numbers are passed to the function each time. I assumed that I first took β€œ25” (a) and β€œ8” (b), and then β€œ7” (a) and β€œ41” (b), so:

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

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

How are two sets of numbers then sorted in relation to each other?

Please help the struggling newbie!

+61
javascript sorting comparator
Sep 29 '09 at 20:21
source share
6 answers

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

Yes

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

You can recognize yourself with:

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

EDIT

This is the output I got:

 25,8 25,7 8,7 25,41 
+36
Sep 29 '09 at 20:27
source share
β€” -

The JavaScript interpreter has a sort of sorting algorithm built into it. It calls the comparison function several times during the sort operation. The number of calls made by the comparison function depends on the particular algorithm, the data to be sorted, and the order that it is before sorting.

Some sorting algorithms do not work well on already sorted lists, because they force them to do much more comparisons than in a typical case. Others do well with pre-sorted lists, but have other cases where they can be tricked into poor execution.

There are many sorting algorithms that are not used, since no algorithm is ideal for all purposes. The two most commonly used for general sorting are Quicksort and merge sort . Quicksort is often the faster of the two, but merge sorting has some nice features that can make it a better general choice. The merge sort is stable , but Quicksort is not. Both algorithms are parallel, but the merge sort method makes parallel implementation more efficient, all other things being equal.

Your particular JavaScript interpreter may use one of these algorithms or something else entirely. The ECMAScript standard does not indicate which algorithm corresponds to the execution to use. He even explicitly denies the need for stability.

+35
Sep 29 '09 at 20:27
source share

Pairs of values ​​are compared, one pair at a time. The pair that are being compared is an implementation detail - don't assume that they will be the same in every browser. The callback can be anything (so you can sort strings or roman numerals or something else where you can find a function that returns 1.0, -1).

One thing to consider when sorting JavaScript is that it is not guaranteed to be stable.

+9
Sep 29 '09 at 20:32
source share

Is the array sort callback function many times during sorting?

Yes, that's for sure. The callback is used to compare pairs of elements in the array as necessary, to determine in which order they should be. This implementation of the comparison function is not atypical when working with numerical sorting. Details in the specification or some other more readable sites.

+4
Sep 29 '09 at 20:26
source share

Is the array sort callback function many times during sorting?

Since this is sorting, sorting, given N elements, the callback function should be called on average (N * Lg N) times for a fast type, such as Quicksort . If the algorithm used is similar to Bubble Sort , then the callback function will be called on average (N * N) times.

The minimum number of calls for sorting sorting is (N-1), and this is only to detect an already sorted list (i.e. early in Bubble Sort, if there is no change).

+3
Sep 29 '09 at 20:35
source share
 var array=[25, 8, 7, 41] array.sort(function(a,b){ console.log('a = ${a} , b = ${b}'); return a - b }); 

EXIT

  • 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 version)) in the first iteration, argument a is the second element in the array, and argument b is the first element of the array.

0
Dec 08 '18 at 5:12
source share



All Articles