Hacker News new | ask | show | jobs
by Ajedi32 2832 days ago
No. `(a,b) => a => b` cannot produce a stable sort in all scenarios. Would theoretically work fine for integers since there's no difference between any two instances of the same number, but consider:

  let arr = [
    {number: 2, name: 'a'},
    {number: 1, name: 'b'},
    {number: 1, name: 'c'},
  ]
Now assume we want to sort arr with a compare function, and our sort function expects the compare function to return a boolean, not an integer.

    let compare = (a,b) => a.number => b.number
    compare(arr[0], arr[1]) //=> true, so the object named 'a' comes after 'b'
    compare(arr[1], arr[2]) //=> true, so the object named 'b' comes after 'c'?
See the problem?