-2

I got question today, that I couldn't answer. I would appreciate if you could just explain it to me. Why my array doesn't output [1, 2, 3, 4, 5] but only [2, 3, 4, 5]? This is the code:

let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
    console.log(a);
});

But when I use this code, it works perfectly fine

let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
    return a;
})
console.log(order);
3
  • The callback is called in order to execute a sorting algorithm. There is no guarantee as to which the arguments it will be called with nor in which order. You should not need to rely on that at all. Commented May 2, 2021 at 16:45
  • 1
    The MDN docs have an excellent explanation of how the comparator function is meant to work here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 2, 2021 at 16:47
  • The sorter does not need that information. The first entry is most probably entered as the parameter b against some other number. Commented May 2, 2021 at 16:53

2 Answers 2

2

It doesn't output [2, 3, 4, 5] it outputs 2, 3, 4 and 5 each in a separate step of the iteration. If you also log order you'll see that the array still contains [1, 2, 3, 4, 5]. The comparator function receives two elements of the array that it compares to each other. That way it knows in which order those elements should be. If you only log a and not b, you'll wont see all the information that is passed to the comparator. If you log both a and b, you'll see that also 1 is passed, to parameter b.

let numbers = [1, 2, 3, 4, 5]
let order = numbers.sort((a, b) => {
    console.log('Comparing', a, b);
});

console.log('Final result', order);

Keep in mind that the comparator function should return either a positive number when a is greater than b, a negative number when a is less than b and 0 when both elements are equal to each other. The above comparator function doesn't return any value at all, so it might not do what you expect it to do.

(Also your snippet that "works perfectly fine" doesn't follow that contract. It returns a positive number for each element, so it is basically saying that every element is greater than every other element.)

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I solved it by myself by returning a - b, or b -a dephending on order you want. It's just happenede that I didn't quite understand the situation here going on. Thanks
Glad you solved it. Since you're somewhat new here, I want to remind you that you can mark one answer as accepted by clicking on the green tick at the left of the answer. Don't feel pressured though. Whether you do or don't accept an answer is up to you.
Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded.
@Ceki For upvoting (clicking the upwards arrow icon) you indeed need 15 reputation. Marking a question as accepted (clicking the green tick), doesn't require that.
I did it. Thank you.
0

The sort() method sorts the elements of an array in place and returns the sorted array.

[1, 2, 3, 4, 5].sort((a, b) => {
  console.log({a, b}); // a is secondItem, b is firstItem of an array
  return -1; // return -ve value for DESC and truthy value for ASC order
});

Output will be

{a: 2, b: 1}
{a: 3, b: 2}
{a: 4, b: 3}
{a: 5, b: 4}

sort output: [5, 4, 3, 2, 1]

2 Comments

Thanks for answer, but previous one helped me better.
Good to know :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.