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 164 views
1 answer 139 views
1 answer 121 views
2 answers 222 views
1 answer 156 views
4 answers 407 views
...