News for hackers. Stuff that matters.
Re:Blessed Are the JavaScript Developers by ImpressiveWebs on Monday August 31, 2020 @12:38PM (#24333127)
Just read the docs:

> If `compareFunction` is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:Blessed Are the JavaScript Developers by Gaelan on Monday August 31, 2020 @12:58PM (#24333398)
"it's documented" is no excuse for unexpected behavior.

(Relevant xckd: https://xkcd.com/293/)

Re:Blessed Are the JavaScript Developers by danShumway on Monday August 31, 2020 @02:04PM (#24334175)
I'm not sure it's unexpected.

Javascript has some weird behaviors, but in this case Arrays in Javascript can contain multiple types. How would you sort an array of `['cat', 80.3, '80']`? I think I would expect that to end up as `['80', 80.3, 'cat']`, not `[80.3, '80', 'cat'].

Or to make things more complicated, `['cat', 80.3, { foo: 'bar' }, true]`. Do we want to try and intuit whether that object gets converted to a string or a value? Do we base that intuition on the prototype chain, or what?

I think it makes sense to cast every object/type in the array to a shared format so everything can be compared based on the same criteria.

---

Have simple rules that are universally applied:

- All objects are converted to strings

- Strings are sorted alphabetically

vs something weird like:

- We try to intuit what type you want based on several common type conversions and possibly the prototype

- We have a set of built-in rules about which primitive types (number, boolean, string, Symbol) comes before and after other primitive types.

Re:Blessed Are the JavaScript Developers by Gaelan on Monday August 31, 2020 @06:19PM (#24336477)
Personally, I'd expect sorting non-homogenous arrays to either sort by type first, or (even better, IMO) throw an exception.
Re:Blessed Are the JavaScript Developers by naikrovek on Monday August 31, 2020 @06:21PM (#24336487)
> I'm not sure it's unexpected.

What do YOU expect when you ask someone to sort items that are clearly numbers?

I don't know about you, but I do not expect what actually happened.

Re:Blessed Are the JavaScript Developers by danShumway on Monday August 31, 2020 @07:20PM (#24336887)
We don't know that the items are clearly numbers, arrays in Javascript are untyped.

Your proposal is that Javascript's default sort algorithm should change behaviors based on whether it can do an extra O(n) loop over the entire array and check whether `typeof x === 'number'` evaluates to true for each index? I don't think that's a good idea.

Re:Blessed Are the JavaScript Developers by clktmr on Monday August 31, 2020 @01:45PM (#24333981)
Well, what would be the expected behavior of sorting an untyped array?
Re:Blessed Are the JavaScript Developers by jlokier on Monday August 31, 2020 @02:07PM (#24334199)
Using whatever the built-in '<' operator does would be more natural than converting to strings just for sort().
Re:Blessed Are the JavaScript Developers by clktmr on Monday August 31, 2020 @02:31PM (#24334488)
I bet not!

  1 > {} // false
  1 < {} // false
So the result of sorting will be random.

People are fast to complain..

Re:Blessed Are the JavaScript Developers by asdf-asdf-asdf on Monday August 31, 2020 @01:08PM (#24333525)
unfortunately it's not possible to change the behavior because of backward compatibility.

maybe we could have a `sort2()` method that works differently.

Re:Blessed Are the JavaScript Developers by brundolf on Monday August 31, 2020 @03:24PM (#24335056)
Yes, this would be the standard JavaScript approach to this kind of problem. See ===, "let", backtick-strings, Map, and most recently, Temporal.
Re:Blessed Are the JavaScript Developers by SwiftyBug on Monday August 31, 2020 @01:35PM (#24333862)
`actuallySort()`