faqts : Computers : Programming : Languages : JavaScript : Language Core : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 12 people (58%) answered Yes
Recently 4 of 9 people (44%) answered Yes

Entry

How can I sort an array of strings by the length of the elements
How can I sort an array of strings by the length of the elements

Mar 13th, 2000 04:39
Martin Honnen,


Use a suitable comparison function e.g.
  function compareLength (el1, el2) {
    return el1.length - el2.length;
  }
which you pass to the Array sort method:
  //example
  var a = new Array('Kibology', 'Maho', 'K')
  a.sort(compareLength);
  alert(a)