1

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

Sebastian Celis
12.2k6 gold badges38 silver badges45 bronze badges
asked Apr 28, 2009 at 15:57
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.

answered Apr 28, 2009 at 15:59
Sign up to request clarification or add additional context in comments.

Comments

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"]
answered Apr 28, 2009 at 16:06

1 Comment

I was about to write the actual function. You beat me to it. +1
2

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

answered Apr 28, 2009 at 16:01

Comments

0

use this :

array.sort(function(el1,el2){
 return el1.length - el2.length
});
answered Dec 15, 2017 at 12:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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