-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);
Ceki
  • 31
  • 1
  • 6
  • 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. – trincot May 02 '21 at 16:45
  • 1
    The MDN docs have an excellent explanation of how the comparator function is meant to work here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description – kingkupps May 02 '21 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. – loop May 02 '21 at 16:53

2 Answers2

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

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 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 – Ceki May 03 '21 at 09:55
  • Glad you solved it. Since you're somewhat new here, I want to remind you that [you can mark one answer as accepted](https://stackoverflow.com/help/someone-answers) 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. – Ivar May 03 '21 at 10:04
  • Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded. – Ceki May 03 '21 at 10:39
  • @Ceki For upvoting (clicking the upwards arrow icon) you indeed need 15 reputation. Marking a question as accepted ([clicking the green tick](https://meta.stackexchange.com/a/5235)), doesn't require that. – Ivar May 03 '21 at 10:41
  • I did it. Thank you. – Ceki May 03 '21 at 17:14
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]
Zuhair Naqi
  • 1,162
  • 11
  • 21