javascript sort function parameters

When I was learning about js functions, I came across an example of using a function as a return value. In this example, the return value was passed as a parameter to the sort() function to execute. In fact, I have also encountered the sort() function before. Formal parameters, I just didn't care about it at the time, but I encountered it again today, so I want to figure it out...

The sort() function has clearly stated that it can have a formal parameter in the introductory tutorial of w3school, and if there is a parameter, this parameter must be a function, and the corresponding example is also given:

Definition and Usage
The sort() method is used to sort the elements of an array.

grammar
arrayObject.sort(sortby)
Parameter Description
sortby optional. Specifies the sort order. Must be a function.

Examples of incorrect sorting:

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort())

</script>
output:

10,5,40,25,1000,1
1,10,1000,25,40,5

Sorting correct example:

<script type="text/javascript">

function sortNumber(a,b)
{
return a - b
}

var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"

document.write(arr + "<br />")
document.write(arr.sort(sortNumber))

</script>
output:

10,5,40,25,1000,1
1,5,10,25,40,1000

When I saw this, my previous doubt was, where is the value to pass parameters to the sortNumber function? And how does the return value of the sortNumber function affect the sort function?

In fact, the big guy on Zhihu has explained it with easy-to-understand code:

Know the answer to the sort() function

If you want to really understand the formal parameters of this function, you should look at the source code of V8 about array sorting (also js code):

     function InsertionSort(a, from, to) {
          for (var i = from + 1; i < to; i++) {
          var element = a [i];
          for (var j = i - 1; j >= from; j--) {
            var tmp = a[j];
            var order = comparefn(tmp, element);
            if (order > 0) {
              a[j + 1] = tmp;
            } else {
              break;
            }
          }
          a[j + 1] = element;
        }
      };

This js code is the code for insertion sort on V8 (the method is used for sorting the array length is less than 10 in V8). The comparefn function is the formal parameter (function) of the sort function you passed in. This code can It can be seen that the formal parameter of the sort function (that is, the parameter of the sortNumber function above) is to pass two numbers in the array in the function implementation; and the return value (the return value of the sortNumber function above) The function is to tell the sort function how you want to sort the two numbers, and then the sort function operates on the array based on the returned information.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649510&siteId=291194637