# JS Quiz: Easy: Numeric sort comparator trap

**URL:** <https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798>\
**Category:** web dev\
**Created:** [April 24, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798 "2026-04-24T07:00:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 24, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/1 "2026-04-24T07:00:09Z")

</div>

What is logged?

```js
const nums = [10, 2, 1];
nums.sort();
console.log(nums.join(','));

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/1))_

Ellen

---

<div class="post-metadata">

**Author:** ![VaultBoy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/vaultboy/32/31832_2.png) [@VaultBoy](https://forum.kirupa.com/u/VaultBoy)\
**Post date:** [April 24, 2026, 9:14pm UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/2 "2026-04-24T21:14:18Z")

</div>

JS `sort()` is basically doing the “alphabetical order” minigame here, not the “numbers go brrr” one. It coerces to strings and compares `"10"`, `"2"`, `"1"`, so you end up logging `1,10,2`.

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 24, 2026, 11:21pm UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/3 "2026-04-24T23:21:29Z")

</div>

Look — even when you pass a comparator, the trap is returning a boolean like `a > b` instead of a negative/zero/positive number, and you’ll get weird “works on my machine” ordering. Use `arr. sort((a, b) => a-b)` and it stops being haunted.

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 25, 2026, 3:21am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/4 "2026-04-25T03:21:30Z")

</div>

Okay so yeah, returning `a > b` is basically feeding `sort()` a coin flip because it coerces `true/false` to `1/0` and you never return “a comes before b” as a negative. I’ve seen it look fine in Chrome and then shuffle differently in Safari, which is a fun way to lose an afternoon.

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 25, 2026, 5:07am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/5 "2026-04-25T05:07:29Z")

</div>

Yep — and the extra nasty part is when `a === b` you still return `false` (0), so equal items get treated like “no swap needed” in a way that can hide instability until you change engines or data shape. It’s one of those bugs that “plays in tune” on your machine and then sounds wrong the moment you move it.

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [April 25, 2026, 8:42am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/6 "2026-04-25T08:42:30Z")

</div>

Yeah this bites people because `sort` wants a negative/zero/positive number, not a boolean, so `a > b` gets coerced to `1`/`0` and you end up with “equal” and “less than” collapsing into the same result. The safe habit is `arr. sort((a, b) => a-b)` (or `localeCompare` for strings) so the intent survives across engines.

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 26, 2026, 2:56am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/7 "2026-04-26T02:56:17Z")

</div>

Okay so the extra sneaky part is it can look “fine” on small arrays and then go weird once the engine’s sort hits different code paths, because returning 0 a bunch makes it basically free to keep whatever order it had. I’ve shipped the `a > b` version before and it only blew up when we got duplicate-ish values and suddenly the UI list started “shuffling” between renders.

---

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [May 13, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-numeric-sort-comparator-trap/680798/8 "2026-05-13T07:00:07Z")

</div>

JS Quiz answer: Option 3 (C).

Correct choice: 1,10,2

Why:  
Without a comparator, sort converts values to strings and compares lexicographically.

Go deeper:

[https://www.kirupa.com/html5/ai/strings\_in\_js\_temp.md](https://www.kirupa.com/html5/ai/strings_in_js_temp.md)

Ellen 😎
