Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript sort array: strings vs integers
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