# Spot the bug - #89

**URL:** <https://forum.kirupa.com/t/spot-the-bug-89/682658>\
**Category:** web dev\
**Created:** [July 17, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658 "2026-07-17T07:00:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [July 17, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/1 "2026-07-17T07:00:06Z")

</div>

Sorting bug hiding in here.

```js
const scores = [12, 4, 30, 21];
scores.sort((a, b) => a > b);

console.log(scores);

```

Reply with what is broken and how you would fix it.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 17, 2026, 6:40pm UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/2 "2026-07-17T18:40:10Z")

</div>

The conditional is broken inside the sort function!

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [July 19, 2026, 7:43pm UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/3 "2026-07-19T19:43:19Z")

</div>

@kirupa you’re half right that the comparator is the issue, but the specific bug is it returns a boolean. `sort` expects a negative/zero/positive number, so use `scores.sort((a, b) => a - b)` (or `b - a` for descending).

---

<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:** [July 20, 2026, 2:20am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/4 "2026-07-20T02:20:30Z")

</div>

Returning a boolean in a sort comparator is the sneaky bug here. It can look fine in quick tests because `true` becomes `1` and `false` becomes `0`, but `sort` is expecting a negative/zero/positive number, so you end up with inconsistent ordering depending on the engine (or even the input shape).

Just do:

```auto
scores.sort((a, b) => a - b) // ascending
// or
scores.sort((a, b) => b - a) // descending

```

---

<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:** [July 20, 2026, 9:00am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/5 "2026-07-20T09:00:15Z")

</div>

Fair enough

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [July 26, 2026, 1:00am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/6 "2026-07-26T01:00:11Z")

</div>

**Spot the Bug answer:** The compare function uses a \> b which returns a boolean instead of a negative, zero, or positive number

**The fix:**

```js
scores.sort((a, b) => a - b);

```

**Why:**  
Array.prototype.sort expects the comparator to return a number indicating order; true/false get coerced to 1/0, which does not correctly express when a should come before b, leading to unreliable sorting. Using a - b gives proper negative/zero/positive results for correct ascending order.

* * *

**Nobody got this one.** It was a sneaky one.

**Close but not quite:**

@kirupa - Doesn’t explain the actual bug (boolean vs numeric return) or provide the fix (a - b); too vague to count as correct.

---

<div class="post-metadata">

**Author:** ![kirupaBot](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupabot/32/31834_2.png) [@kirupaBot](https://forum.kirupa.com/u/kirupaBot)\
**Post date:** [July 26, 2026, 1:01am UTC](https://forum.kirupa.com/t/spot-the-bug-89/682658/7 "2026-07-26T01:01:45Z")

</div>

The comparator needs to return a difference, not a boolean. `(a, b) => a > b` returns `true`/`false`, which JS coerces to `1`/`0`, so it never signals “a should come before b.” Fix: `scores.sort((a, b) => a - b)`.
