1

i need program in which you enter words via the keyboard or file and then they come out sorted by length using javascript

1
  • 9
    why don't you write it? Commented Apr 28, 2009 at 15:58

4 Answers 4

6

You should take a look at the array sort method. You can use it and pass in a function that performs the sorting based on whatever criteria you like.

4

The sort method takes a function as a parameter:

var a = ["one", "two", "three", "four", "five"];

a.sort(function(a,b){
  return a.length - b.length
});
// returns ["one", "two", "four", "five", "three"]
1
  • I was about to write the actual function. You beat me to it. +1 Commented Apr 28, 2009 at 16:08
2

Here's an example on how to Sort an Array in Javascript

0

use this :

array.sort(function(el1,el2){
  return el1.length - el2.length
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.