I don't understand why numeric sort functions work in Javascript

I understand that the .sort () method sorts the array alphabetically, as if it were a string. Even if your array consists solely of numbers.

I also understand that to sort the array numerically, the code is:

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

The problem is that I do not understand WHY this works. The function I wrote should take two arguments: "a" and "b", but where do these arguments come from? An array? How does he know that? Then the function returns a certain number. If "a" and "b" were 10 and 5, respectively, my function would return "5". Now I do myArray.sort (5); How the hell does it magically sort my entire array numerically?

Thanks for the help.

+4
source share
7 answers

The function I wrote should take two arguments: "a" and "b", but where do these arguments come from?

To determine which elements of the order array belong to, the function Array.sortselects pairs of elements from your array and passes them to your comparison function as awell b. The result returned by your function determines in what order the two elements are placed - if it is negative, then it knows what to put abefore b; if he is positive, he knows what bto put after a. The actual number does not matter if it is just negative or positive.

A slightly less confusing way of writing your example might be:

function myComparisonFunction(a, b) {
    if (a < b) {
        return -1;
    }
    if (a > b) {
        return 1;
    }
    return 0;
}

myArray.sort(myComparisonFunction);
+5
source

(), . , . ? sort

  • -1, ;
  • 1, ;
  • 0, ( , *

, , .

, JavaScript, . , JavaScript quicksort.

* , : , , , ; , "" , , "" , , . , , , , 0. , , . JavaScript undefined .

+3

, Array.sort ( ES5, MDN), , ..

  • a < b, 0
  • a > b, 0
  • else (a == b) return 0

, :

a     b     a-b    meaning (see above)
----  ----  ----   ------
10    5     5      "a > b"
5     10    -5     "a < b"
5     5     0      "a == b" 

, :

- , ( " " [aka "" ]), .

(. Array.sort stable.)

+1

myArray.sort(5);

. , () 5, , 5.

, , sort , , . sort . , , " "; , , " "; , , " ". ( β€” , mdash, C.) , a-b, .

, , sort , , . , , 1 2 2 3, , 2 1, 3 , 2, 1 3, 3 1. .

( , MDN Array.prototype.sort. , .)

+1

, , . , , - , "" .

, , javascript. javascript . , , ( jquery, , , ajax, ... , , 500px, , , 500 , ).

, a b , , .

, , :

sales:[{sale:{"brand":"Nike", "person":"Eric", "total":5000}}, {sale:{...}}]

, :

sales.sort(function(a,b){
    if (a.brand==b.brand){
       return a.brand>b.brand ? 1 : -1;
    } else {
       return a.total-b.total;
});

, ... , , -, , , , , , "", .

+1

a and b are two consecutive values ​​in the array for comparison and subtraction can be 0, less than zero and more than zero. if it is greater than zero, this means that the previous element is larger, so it needs to be pushed further down; if it is less than zero, it means that it does not need to be pushed down.

0
source

In your example, you are NOT executing myArray.sort(5);because you are passing a function (which is used to specify a sorting algorithm), not a number.

0
source

Source: https://habr.com/ru/post/1525975/


All Articles