// this is totally fine [15, true, 'Unobtanium', [2, 4], null, 81]
['cucumber', 'apple', 'banana'].sort() // ⇒ ['apple', 'banana', 'cucumber']
This is pretty straightforward so far. Check out how it sorts the numbers 1 through 10:
[3, 5, 6, 8, 10, 7, 2, 1, 9, 4].sort() // ⇒ [1, 10, 2, 3, 4, 5, 6, 7, 8, 9]
undefined which always ends up last):
['trauma', null, 'why', true, undefined, false].sort() // ⇒ [false, null, 'trauma', true, 'why', undefined]
[3, 5, 6, 8, 10, 7, 2, 1, 9, 4].sort((a, b) => a - b) // ⇒ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
To sort numbers from largest to smallest, we swap
[3, 5, 6, 8, 10, 7, 2, 1, 9, 4].sort((a, b) => b - a) // ⇒ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
For most other cases, the correct sorting will depend on your data and the domain you work in. MDN has another few examples to check out:
The bite-sized tips I started sharing on Twitter show how code can be improved gradually. You can read them all on this site now.
Instead of navigating many layers of directories when importing files, we can use aliases that cover that error-prone part for us.
Instead of ignoring the same files specific to your machine in every project, exclude them globally once for all your projects.