What is the principle of sort sorting in js?

for example, the following code:

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

the result of execution is:
10, code 5, 40, 50, 50, 000, 1
1, 5, 10, 25, 40, 1000

Why is the result of the second line output ascending rather than descending? What do the an and b in sortNumber stand for, respectively? The value of amurb should not be fixed, how to pass it into sort and how to judge the sort? Hope to give a detailed explanation, thank you!

Dec.03,2021

borrow a wave of answers from Baodi
as long as you know that some two values in the array are passed into the function in sort and the returned value will determine the way the array is reorganized (a little bit around)
and then copy the code to the console

The
  

sort method accepts a comparator , which compares the two parameters passed in: a and b . a > b returns positive numbers , a < b returns negative numbers , a negative numbers returns 0 . Then sort calls the internally implemented comparison algorithm, and with this comparator , it can sort.

Menu