TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Curious Case of JavaScript’s `sort`

23 pointsby hoovabout 9 years ago

14 comments

wscottabout 9 years ago
Yes yes, everyone one is saying &quot;of course&quot; these are string compares! What a nub mistake. But is it?<p>It is a very easy mistake to make, you pick &#x27;sort&#x27; and the resulting code does what you expect. As long as all the timet&#x27;s have the same number of digits then the string comparison works. You have to have a data that crosses 9&#x2F;8&#x2F;2001 to see the problem. (or 3&#x2F;3&#x2F;1973 before that)<p>The javascript compiler isn&#x27;t going to tell you about this and it only hits a problem with dates older than most programs that are written in javascript.
评论 #11488989 未加载
评论 #11488983 未加载
meeslesabout 9 years ago
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.
评论 #11489275 未加载
评论 #11491373 未加载
gpvosabout 9 years ago
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.)
SimeVidasabout 9 years ago
Why is scrolling so slow in Firefox on that page?
评论 #11491382 未加载
Domenic_Sabout 9 years ago
MySQL exhibits similar behavior if you&#x27;re comparing numbers stored in a field with string-ish type (eg, varchar):<p><pre><code> mysql&gt; select cast(123 as char(255)) &gt; &quot;2&quot;; +------------------------------+ | cast(123 as char(255)) &gt; &quot;2&quot; | +------------------------------+ | 0 | +------------------------------+ 1 row in set (0.00 sec) </code></pre> It can really bite you if you&#x27;re filtering queries on &quot;WHERE char_field &gt; 10&quot;. I personally did not think a lot about datatypes after initial table creation... until I ran into this.
simlevesqueabout 9 years ago
Stop hijacking my scrollwheel.
witty_usernameabout 9 years ago
So if I&#x27;m understanding correctly, JS sort sorts numbers by their string value? That&#x27;s crazy. And it seems you have to add a compare function just to do a basic operation to sort numbers.
评论 #11488719 未加载
评论 #11489627 未加载
评论 #11488795 未加载
评论 #11488658 未加载
apoabout 9 years ago
Also see:<p><a href="http:&#x2F;&#x2F;blog.rodneyrehm.de&#x2F;archives&#x2F;14-Sorting-Were-Doing-It-Wrong.html" rel="nofollow">http:&#x2F;&#x2F;blog.rodneyrehm.de&#x2F;archives&#x2F;14-Sorting-Were-Doing-It-...</a>
spiderfarmerabout 9 years ago
You should have read W3Schools:<p>&gt; However, if numbers are sorted as strings, &quot;25&quot; is bigger than &quot;100&quot;, because &quot;2&quot; is bigger than &quot;1&quot;.
评论 #11488691 未加载
评论 #11488751 未加载
sriram_iyengarabout 9 years ago
have seen people raising big uffs-oohs-aahs because of stackoverflow-driven-development :)<p>developer mozilla helps <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Array&#x2F;sort" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a><p>unit tests help !
js2about 9 years ago
The suggestion:<p><pre><code> function compareNumbers(a, b) { return a - b; } </code></pre> Can this not suffer from overflow&#x2F;underflow?
评论 #11488927 未加载
评论 #11488951 未加载
gingerrrabout 9 years ago
ITT: people who expect a language to work just like they want it to instead of reading the docs for the language.
cristiantincuabout 9 years ago
Spec:<p>1. <a href="http:&#x2F;&#x2F;www.ecma-international.org&#x2F;ecma-262&#x2F;6.0&#x2F;#sec-array.prototype.sort" rel="nofollow">http:&#x2F;&#x2F;www.ecma-international.org&#x2F;ecma-262&#x2F;6.0&#x2F;#sec-array.pr...</a><p>2. <a href="http:&#x2F;&#x2F;www.ecma-international.org&#x2F;ecma-262&#x2F;6.0&#x2F;#sec-sortcompare" rel="nofollow">http:&#x2F;&#x2F;www.ecma-international.org&#x2F;ecma-262&#x2F;6.0&#x2F;#sec-sortcomp...</a>
CJeffersonabout 9 years ago
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&#x27;s default sort is, and (b) how none of the famous libraries (underscore&#x2F;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 &#x27;lexicographic&#x27; with &#x27;element-wise&#x27;, as I think it might be causing confusion.
评论 #11489042 未加载
评论 #11488752 未加载
评论 #11488800 未加载