list of names in a string, how could I sort them alphabetically? JAVASCRIPT

38 views Asked by At

so there is a primitive simple string

let friends = 'Moe,Larry,Curly,Jane,Emma,Elizabeth,Elinor,Mary,Darcy,Grey,Lydia,Harriet';

I want that string to be converted in Alphabetical order (a,b,c,d)

[
  'Emma',  
  'Elizabeth',  
  'Elinor', 
   etc...
]

What I`ve tried are:

let x = friends.sort() // < didnt work

let obj = Object.values(friends) 

M o e , L a r r y , C u r l y , J a n e , E m m a , E l i z a b e t h , E l i n o r , M a r y , D a r c y , G r e y , L y d i a , H a r r i e t

Kinda cool, but this is not what I exactly need

1

There are 1 answers

0
Eric Fortis On

let friends = 'Moe,Larry,Curly,Jane,Emma,Elizabeth,Elinor,Mary,Darcy,Grey,Lydia,Harriet';

friends = friends.split(',').sort()
console.log(friends)