# How does sorting array numbers works?

**URL:** <https://forum.freecodecamp.org/t/how-does-sorting-array-numbers-works/389639>\
**Category:** General\
**Created:** [May 16, 2020, 4:12am UTC](https://forum.freecodecamp.org/t/how-does-sorting-array-numbers-works/389639 "2020-05-16T04:12:29Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 16, 2020, 9:52am UTC](https://forum.freecodecamp.org/t/how-does-sorting-array-numbers-works/389639/3 "2020-05-16T09:52:01Z")

</div>

> [@Arr.sort((a,b) =\> a-b); explanation](http://forum.freecodecamp.org/t/arr-sort-a-b-a-b-explanation/167677/11):
>
> Just use debugger and watch the values. It’s not top secret. (() =\> { const sorted = [3,6,9,4,6].sort(function(a, b) { return a - b; }); console.log(sorted); })(); 1st iteration: a = 6, b = 3 // return 3 2nd iteration: a = 9, b = 6 // return 3 3rd iteration: a = 4, b = 9 // return -5 4th iteration: a = 4, b = 6 // return -2 5th iteration: a = 4, b = 3 // return 1 6th iteration: a = 6, b = 6 // return 0 7th iteration: a = 6, b = 9 // return -3 [3] [6] [ 9 …

---

_[View the full topic](https://forum.freecodecamp.org/t/how-does-sorting-array-numbers-works/389639)._
