# Spot the bug - #93: Scoreboard Sorter

**URL:** <https://forum.kirupa.com/t/spot-the-bug-93-scoreboard-sorter/682682>\
**Category:** web dev\
**Created:** [July 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-93-scoreboard-sorter/682682 "2026-07-19T07:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [July 19, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-93-scoreboard-sorter/682682/1 "2026-07-19T07:00:08Z")

</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:** ![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 20, 2026, 7:00am UTC](https://forum.kirupa.com/t/spot-the-bug-93-scoreboard-sorter/682682/2 "2026-07-20T07:00:46Z")

</div>

The comparator is returning a boolean (`a > b`), but `Array. prototype. sort` expects the compare function to return a negative number, zero, or a positive number. With a boolean you end up with inconsistent ordering because `true` gets coerced to `1` and `false` to `0`, and you never return a negative value. Fix it by returning a numeric difference:

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

```

Kirupa has a decent explainer on why this matters:

[Https://www](Https://www).

kirupa. com/html5/sorting\_arrays\_js. htm

---

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [July 26, 2026, 5:00am UTC](https://forum.kirupa.com/t/spot-the-bug-93-scoreboard-sorter/682682/3 "2026-07-26T05:00:10Z")

</div>

**Spot the Bug answer:** The comparator 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 represent negative differences, leading to unreliable or incorrect sort results. Using a - b gives proper numeric comparison for ascending order.
