How to sort array of strings alphabetically in JavaScript

1 Answer

0 votes
let arr = ['php', 'javascript', 'c', 'python', 'c++'];

arr = arr.sort((a, b) => a.localeCompare(b));

console.log(arr);
  
    
    
    
/*
run:
    
["c", "c++", "javascript", "php", "python"]
    
*/

 



answered Feb 3, 2021 by avibootz

Related questions

1 answer 248 views
1 answer 229 views
1 answer 206 views
2 answers 339 views
1 answer 233 views
4 answers 580 views
...