Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js how to sort array by string length

(() => {
  let arr = ["HTML", "Go", "Ruby", "Python", "JavaScript", "CSS"];
  function sortByLength(arr) {
    return arr.sort((a, b) => a.length - b.length);
  }

  console.log(sortByLength(arr)); // => [ 'Go', 'CSS', 'HTML', 'Ruby', 'Python', 'JavaScript' ]
})();
 
PREVIOUS NEXT
Tagged: #js #sort #array #string #length
ADD COMMENT
Topic
Name
2+4 =