How to sort an array by string length in ascending order with TypeScript

1 Answer

0 votes
let arr = ['php', 'typescript', 'c', 'python', 'c++', 'c#'];
 
arr = arr.sort((a,b) => a.length - b.length);
 
console.log(arr); 
 
 
   
     
     
/*
run:
     
["c", "c#", "php", "c++", "python", "typescript"] 
     
*/

 



answered Aug 6, 2022 by avibootz
...