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

1 Answer

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

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

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

 



answered Feb 3, 2021 by avibootz
...