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
nana
-
9why don't you write it?SilentGhost– SilentGhost2009年04月28日 15:58:14 +00:00Commented Apr 28, 2009 at 15:58
4 Answers 4
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
Sebastian Celis
12.2k6 gold badges38 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Borgar
38.9k5 gold badges44 silver badges42 bronze badges
1 Comment
Jose Basilio
I was about to write the actual function. You beat me to it. +1
Here's an example on how to Sort an Array in Javascript
answered Apr 28, 2009 at 16:01
Jose Basilio
51.7k13 gold badges124 silver badges117 bronze badges
Comments
use this :
array.sort(function(el1,el2){
return el1.length - el2.length
});
Comments
lang-js