How to sort array of strings by string length (size) from min to max length in JavaScript

1 Answer

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

arr.sort(function (a, b) {
  return a.length - b.length;
});

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

  

 



answered Feb 3, 2021 by avibootz
...