Skip to content

Conversation

patgarcia
Copy link

According to MDN docs on Array.prototype.sort

The default sort order is ascending, built upon converting the elements into strings

[100,2].sort()
// [100, 2]

So we would need the compare function to sort numbers properly

[100,2].sort((a,b) => a - b)
// [2, 100]

According to MDN docs [on Array.prototype.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
>The default sort order is ascending, built upon converting the elements into strings
```javascript
[100,2].sort()
// [100, 2]```
So we would need the compare function to sort numbers properly
```javascript
[100,2].sort((a,b) => a - b)
// [2, 100]```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant