Skip to content
DevMeme
1850 of 7590
Languages Post #2058 · source on Telegram

JavaScript's Unique Interpretation of 'Sorting'

Description

This meme uses the popular 'Laughing Leo' format, featuring a smug-looking Leonardo DiCaprio from the film 'Django Unchained' holding a drink. The meme is divided into two parts. The top text reads: 'People learning JavaScript: "I'll use array.sort() to sort this list of numbers!"'. Below this, the text 'JavaScript:' introduces the punchline. The image of Leonardo DiCaprio has an array overlaid on it: '[1, 100000, 21, 30, 4]'. A small watermark for 't.me/dev_meme' is visible at the bottom. The humor stems from a classic JavaScript quirk where the default `Array.prototype.sort()` method sorts elements lexicographically (as strings) rather than numerically. This results in '100000' coming before '21' because the character '1' precedes '2'. For experienced developers, this is a rite of passage and a well-known 'gotcha' that highlights the language's often counter-intuitive behavior and the importance of providing a custom comparator function (`(a, b) => a - b`) for numerical sorting

Comments

7
Anonymous ★ Top Pick The JavaScript `sort()` method is a great interview question. It efficiently sorts candidates into two buckets: those who have been burned by it, and those who are about to be
  1. Anonymous ★ Top Pick

    The JavaScript `sort()` method is a great interview question. It efficiently sorts candidates into two buckets: those who have been burned by it, and those who are about to be

  2. Anonymous

    Array.sort() with no comparator is ECMAScript’s built-in chaos monkey - behaves in unit tests, then lexicographically reorders your revenue numbers at 2 a.m

  3. Anonymous

    Twenty years later, we're still explaining to junior devs why their sort comparator needs to be `(a, b) => a - b`, right after we finish explaining why `0.1 + 0.2 !== 0.3` and just before the inevitable "wait, why is typeof null 'object'?" conversation

  4. Anonymous

    Ah yes, the classic JavaScript initiation ritual: discovering that array.sort() treats numbers like strings because 'consistency' means everything becomes a string eventually. It's the language's way of teaching you that explicit is better than implicit - usually right after you've deployed to production and your pagination shows page 100000 before page 2. At least it's consistently inconsistent, which is more than we can say for the 47 different ways to define a function

  5. Anonymous

    JavaScript’s Array.sort: stable since ES2019; numeric only if you remember a comparator - otherwise 100000 beats 21 on spelling

  6. Anonymous

    JS array.sort(): where 1000000 humbly precedes 21, a timeless reminder that your metrics dashboard prefers poetry over arithmetic

  7. Anonymous

    Per ES262, sort() compares ToString results - skip the comparator and your “top spenders” dashboard will confidently slot 100000 between 1 and 21; ship (a,b)=>a-b and an ESLint rule

Use J and K for navigation