Skip to content

Instantly share code, notes, and snippets.

@stepheneyer
Created May 1, 2015 16:14
Show Gist options
  • Save stepheneyer/885e52ac5e8b09732155 to your computer and use it in GitHub Desktop.
Save stepheneyer/885e52ac5e8b09732155 to your computer and use it in GitHub Desktop.
Change from String to Array using .split(), then sort
//Create a variable with a string separated by commas
var friends = "Moe,Larry,Curly,Jane,Emma,Elizabeth,Elinor,Mary,Darcy,Grey,Lydia,Harriet";
//Create a new array of the string using the .split() method
//The split method needs the separator such as a comma or space ==> str.split([separator[, limit]])
var newFriends = friends.split([',']);
console.log(newFriends);
//Sort the new array
newFriends.sort();
//==> [ 'Curly',
'Darcy',
'Elinor',
'Elizabeth',
'Emma',
'Grey',
'Harriet',
'Jane',
'Larry',
'Lydia',
'Mary',
'Moe' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment