Yes yes, everyone one is saying "of course" these are string compares! What a nub mistake. But is it?<p>It is a very easy mistake to make, you pick 'sort' and the resulting code does what you expect. As long as all the timet's have the same number of digits then the string comparison works. You have to have a data that crosses 9/8/2001 to see the problem. (or 3/3/1973 before that)<p>The javascript compiler isn't going to tell you about this and it only hits a problem with dates older than most programs that are written in javascript.
Sounds like your solution would have been to properly read documentation of critical sectors of your code. Assuming that just calling `.sort()` on your arbitrary data would give you exactly what you want was a mistake. Not testing this code was also a mistake, because any combination of different length integers would have shown you this problem very early on, saving you some embarrassment and loss of user trust.
Really, their testing was inadequate. If your product is a timeline graph, you should test it with data covering a large range across the possible time values. A single date from before roughly 2000 would have uncovered the problem.<p>(Not that I always test adequately, mind you.)
MySQL exhibits similar behavior if you're comparing numbers stored in a field with string-ish type (eg, varchar):<p><pre><code> mysql> select cast(123 as char(255)) > "2";
+------------------------------+
| cast(123 as char(255)) > "2" |
+------------------------------+
| 0 |
+------------------------------+
1 row in set (0.00 sec)
</code></pre>
It can really bite you if you're filtering queries on "WHERE char_field > 10". I personally did not think a lot about datatypes after initial table creation... until I ran into this.
So if I'm understanding correctly, JS sort sorts numbers by their string value? That's crazy. And it seems you have to add a compare function just to do a basic operation to sort numbers.
Also see:<p><a href="http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-Wrong.html" rel="nofollow">http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-...</a>
You should have read W3Schools:<p>> However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1".
have seen people raising big uffs-oohs-aahs because of stackoverflow-driven-development :)<p>developer mozilla helps
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a><p>unit tests help !
I had some javascript where I wanted to sort a list of list of integers as follows:<p><pre><code> * Sort each inner list smallest to largest
* Sort the list of lists lexicographically
</code></pre>
It amazed that (a) how stupid javascript's default sort is, and (b) how none of the famous libraries (underscore/lodash) seem to have even fixed the problem of arrays not being compared element-wise, or provide an easy drop-in replacement.<p>EDIT: I replaced the word 'lexicographic' with 'element-wise', as I think it might be causing confusion.