How does Javascript's sort() work?

前端 未结 9 1782
长情又很酷
长情又很酷 2020-11-22 11:06

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
})
相关标签:
9条回答
  • 2020-11-22 11:42

    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
    
    0 讨论(0)
  • 2020-11-22 11:43

    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.

    0 讨论(0)
  • 2020-11-22 11:48

    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.

    0 讨论(0)
  • 2020-11-22 11:49

    Deeply Knowledge

    If the result is negative a is sorted before b.

    If the result is positive b is sorted before a.

    If the result is 0 no changes are done with the sort order of the two values.

    NOTE:

    This code is the view inside of the sort method step by step.

    OUTPUT:

    let arr = [90, 1, 20, 14, 3, 55];
    var sortRes = [];
    var copy = arr.slice();		//create duplicate array
    var inc = 0;	//inc meant increment
    copy.sort((a, b) => {
    	sortRes[inc] = [ a, b, a-b ];
    	inc += 1;
    	return a - b;
    });
    var p = 0;
    for (var i = 0; i < inc; i++) {
    	copy = arr.slice();
    	copy.sort((a, b) => {
    		p += 1;
    		if (p <= i ) {
    			return a - b;
    		}
    		else{
    			return false;
    		}
    	});
    	p = 0;
    	console.log(copy +' \t a: '+ sortRes[i][0] +' \tb: '+ sortRes[i][1] +'\tTotal: '+ sortRes[i][2]);
    }

    0 讨论(0)
  • 2020-11-22 11:50
    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.
    0 讨论(0)
  • 2020-11-22 11:51

    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).

    0 讨论(0)
提交回复
热议问题