0

On a function like this one:

Array.sort((a,b) => a - b)

If my array has many items, how does the 'a' and 'b' applies the condition to all of the items ? Because it's the case¹, but it's not the case on a regular function like this one:

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

console.log(isItMatterWhatsMyName(1,2,3,4))

It would apply only on 1 and 2, the 3 & 4 will be forget by the function.

¹ Does it apply only to the sort() or is there more functions that apply this logic ?

Yoël Zerbib
  • 177
  • 2
  • 10
  • Does this answer your question? [How does Javascript's sort() work?](https://stackoverflow.com/questions/1494713/how-does-javascripts-sort-work) – ggorlen Oct 06 '20 at 17:40
  • The comparisons that actually happen are implementation-dependent so all you have to do is guarantee that the sort callback returns < 0, 0 or > 0 per the [contract](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). The second example doesn't make much sense to me because it isn't implementing any sort of logic, nor is `nsm` defined or `isItMatterWhatsMyName` invoked, so I'm not sure where you're going there. – ggorlen Oct 06 '20 at 17:42
  • I mean, I understand that compareFunction acts different than normal function because it follows some algorithms but I can't understand the why of the how ... – Yoël Zerbib Oct 06 '20 at 17:43
  • Sorry the nsm was the originally name of the function, just edited it – Yoël Zerbib Oct 06 '20 at 17:44
  • I believe you're missing that `sort` does all the actual sorting work and the comparator you provide is only invoked as a small step within that sorting routine which typically is implemented as timsort, quicksort alongside insertion sort. If you want to mimic the behavior of `Array#sort`, you need to actually write a sort function that invokes the comparator to determine how to compare any two arbitrary objects `a` and `b` from the array. – ggorlen Oct 06 '20 at 17:45
  • So the logic is that if if follow the implementation-dependant (in this case < 0, 0 or > 0) it will apply it to all of the items of the array. – Yoël Zerbib Oct 06 '20 at 17:46
  • Yes, the sorting function repeatedly inspects two elements in the inner loop of whatever sort function and says "let's ask the user-provided callback how to sort these two elements with respect to each other". See [this](https://stackoverflow.com/questions/52671125/why-does-sorting-a-js-array-of-numbers-with-work/52671435#52671435) for more info although it's not a dupe. The internal code is something like `var order = comparefn(tmp, element);` where `comparefn` is your comparator and `tmp` is `a` and `element` is `b`. – ggorlen Oct 06 '20 at 17:47
  • And we can create a function that apply this logic to all the functions I want to create in order to apply a condition to many functions and just declaring 2 of the many items ? – Yoël Zerbib Oct 06 '20 at 17:48
  • [MDN's documentation of `Array.prototype.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) is pretty explicit and well written. – Heretic Monkey Oct 06 '20 at 17:48
  • Thank you very much for your time now I undersand much more ! Do you know any already existing methods like sort() that apply this logic ? – Yoël Zerbib Oct 06 '20 at 17:48
  • Because the real reason of my question is that I want to find a list of all the functions that applies this logic (would save time, wouldn't it ?) – Yoël Zerbib Oct 06 '20 at 17:50
  • I'm not sure what you mean by "apply this logic"--sorting logic? Using a callback? – ggorlen Oct 06 '20 at 17:50
  • Yes, using only 2 element in the callback and execute them to all of the items of my array – Yoël Zerbib Oct 06 '20 at 17:51
  • That's not really what's happening here and I don't know of any other functions that would do that in any case... Are you under the impression that a sort function is like a pairwise `Array#map` or something? I'd read the suggested links and dupe target thoroughly before continuing the discussion. – ggorlen Oct 06 '20 at 17:52
  • Well you are absolutely right, the function map is doing what I meant... I'm sorry for your time I know I'll find my answers by learning more in the documentation ! Thank you very much. – Yoël Zerbib Oct 06 '20 at 17:56
  • No problem--glad you found out what you wanted. – ggorlen Oct 06 '20 at 18:02

0 Answers0