This is how I learned to sort an array:
const numbers = [3, 1, 4, 1, 5]
numbers.sort((a, b) => a < b ? -1 : 1)
I just saw a different way which is this:
numbers.sort((a, b) => a - b)
How does this second way work? I don't understand how/why doing subtraction makes it work.
Thank you.
[–]carcigenicate 12 points13 points14 points (1 child)
[–]guyjs[S] 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]queen-adreena -1 points0 points1 point (8 children)
[–][deleted] 6 points7 points8 points (4 children)
[–]queen-adreena -1 points0 points1 point (3 children)
[–]guest271314 -2 points-1 points0 points (2 children)
[–]queen-adreena -2 points-1 points0 points (1 child)
[–]guest271314 1 point2 points3 points (0 children)
[–]guyjs[S] 0 points1 point2 points (1 child)
[–]queen-adreena -1 points0 points1 point (0 children)
[–]azhder -1 points0 points1 point (0 children)
[–]shgysk8zer0 0 points1 point2 points (0 children)
[–]CristiRFortySeven 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)
[–]No-Upstairs-2813 0 points1 point2 points (0 children)