Created
September 22, 2021 18:01
-
-
Save kevinhooke/28986702107be921974134e707b23baf to your computer and use it in GitHub Desktop.
JavaScript sort array: strings vs integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arrayOfStrings = [ "bbb", "ccc", "aaa"]; | |
arrayOfStrings.sort(); //sorts as strings in alphabetical ascending order by default | |
//arrayOfStrings = ["aaa", "bbb", "ccc"] | |
let arrayOfInts = [3, 4, 1]; | |
//function passed to sort() returns > 0 if b comes after a, or if < 0 then a comes before b | |
arrayOfInts.sort( (a,b) => a - b ); | |
//arrayOfInts = [1, 3, 4]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment