[javascript] Ask the callback function of the sort function

Asked 2 years ago, Updated 2 years ago, 85 views

The sort function receives the callback function as a parameter. And depending on what the callback function returns, the sort method is different. I'm asking you a question because I don't understand how the sort function works.

const arr = [14,20,18] 
const arr2 = [14,20,18]

arr2.sort(function(a,b){
    console.log(a,b)
    return a-b 
})


arr.sort(function(a,b){
    console.log(a,b)
    if(a>b){
        return 1
    }
    else{ return -1 }
})

We took a, b inside the callback function. We found out that a was the element that came in later, and b was the element that came in first.

Question 0 When I took a console, it came out like that, but is it the element that a came in later, and the element that b came in first? If you look at "https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">

When compareFunction is provided, the array elements are sorted according to the return value of the compare function. If a and b are two elements that are compared, sort a with an index lower than b if compareFunction (a, b) is less than zero. That is, a comes first.

That's what it says. I think a and b in this mdn document is the first index of the element of the sequence, not the element that came in first.

But console.log(a,b) runs four times.

Question 1 I thought a and b were elements of arr. Then shouldn't it be executed only twice in total?

Question 2 return a-b and if(a>b){ return 1 } Why does else{return-1} perform the same function?

Why am I asking this question : I don't know the behavior of the sort function Should I just memorize it...? I don't know what changes when I return a positive number, so I don't know what sorting is.

Thank you for reading my question!

javascript sorting callback

2022-09-21 16:30

2 Answers

Answer to Question 1
Just because there are three elements, you may not be able to complete the desired sorting if you run the alignment twice.

// Simple example. Let's say we sort this array from the smaller one.
shuffled = [2, 996, 1]

// This is what happens when you just sort element zero and element one.
shuffled = [2, 996, 1]

// This is what happens when you just sort elements 1 and 2.
shuffled = [2, 1, 996]

// You can see that it's not aligned properly.

So, I found out that when array.sort() is called, JS traverses array the best number of times required by a particular algorithm. And what that algorithm is... I think it depends on the JS engine.

Even MDN mentions it. Use map() because the memory may pop.

Answer to Question 0
It may or may not be in index order, but it's very messy. When to put in a and b to adjust the original array again is purely the part of the algorithm used. Spin the next sauce.

var items = [9, 8, 5, 7, 6];
items.sort(function (a, b) {
    console.log(a + ", " + b + "\n");
  return a - b;
});
console.log(items);

Answer to Question 2
return a-b and if (a>b) { return1} else { return-1} are not equivalent, but they will eventually do what they want.

var a = 9, b = 9
console.log(a - b) // 0
console.log(a > b ? 1 : -1) // -1

As you can see from reading the document, the logic of branching with a>b is not assumed when a==b because if you do so, the next thing will be annoying as follows.

Note: The ECMAscript standard does not guarantee this behavior, so not all browsers (e.g. Mozilla versions are at least 2003 or later) respect this. (From MDN)

So I think that's how I wrote it to get b out of the way.

Comment on why I'm asking you this question
This is essentially a question of what alignment algorithm JS uses. There's a huge variety of sorting algorithms today, and it's obviously shoveling for users to implement them one by one, and it's really different from time to time to time which of those algorithms is the best. The alignment implementation of JS is a bit... It looks hectic.
If you want to go this deep, you can study engineering about the sorting algorithm or sorting itself that each JS engine uses, or if not, just use whatever you're given and investigate again when you have a problem later.


2022-09-21 16:30

Question 0 I organized it in my own way. (I tried to answer myself again) I'd appreciate it if you could see if this understanding was correct ㅠ<

It is important to organize well because a,b is the opposite of the incoming order and the actual array index.

A comes in later, b comes in first. So on the index, b is the index before, a is the index after.

Compare a and b against specific criteria (although the example below simply compares the size of the values), and returns the values that match the results.

If this result is true, then as in the beginning, a is the index after it, and b is the index before it, so the order of sorting is not changed. If else, the order of a and b changes.

However, in arr, the changed order is not reflected each time the internal function is executed. Question 1: I don't know how that process happens, so I asked you a question ㅠ The sort of arr occurs after all internal functions are called.

arr.sort(function(a,b){
    console.log(a,b)


    If(a>b){ // Compare to criteria. 
        Do not change the index of return 1 // a,b. 
    } 
    The index of else{return-1} // a,b changes. 
})

By the way, return 1 is the index after a, that is, leaving it as it is Return -1 means that the indexes of a and b change!I think it would be a little convenient to use the callback function of sort if you understand .


2022-09-21 16:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.