-2

I have the following arrays:

http://jsfiddle.net/3NZsK/

I need to sort the arrays by their length.

How to find out which one is the largest, the second largest, the third largest, and the least array?

For example, I have the following dummy function, that can get us the second largest array:

http://jsfiddle.net/WPRYf/

What is the above dummy function algorithm?

Thank you in advance

Bayu
  • 437
  • 2
  • 10
  • 19
  • sorry, what? you mean you want to sort the arrays by their length? – gexicide Jun 26 '12 at 11:32
  • I want to find out which one is the largest, the second largest, the third largest and the smallest in quantity. I need to assign them into above variables (please see my explanation) – Bayu Jun 26 '12 at 11:37
  • I think when @gexicide said "What?" he meant that your explanation doesn't make a lot of sense, and he would like you to clarify it. – phenomnomnominal Jun 26 '12 at 11:40
  • Ok. Yes, I want to sort the arrays by their length. How can I do that? – Bayu Jun 26 '12 at 11:43
  • have you tried with length? Determine each array length like this: example: arr_a.length will give the length of the array. – Angel Jun 26 '12 at 11:45
  • I think he wants to find the largest number, the third largest number, and the lowest number in an array. Maybe. He hasn't given enough details – David G Jun 26 '12 at 11:47
  • Yes. I can only get the largest and the smallest array. I need to get the second and the third – Bayu Jun 26 '12 at 11:47
  • @David: No, I want to get the largest, the smallest, the second largest, and the third largest arrays (by their length) – Bayu Jun 26 '12 at 11:49
  • 1
    In that case the second fiddle example is wrong. – Angel Jun 26 '12 at 11:50
  • What do you want to do with that information? What do you do if two arrays have the same, largest length? Same question for smallest length, and for third largest. – Jean Hominal Jun 26 '12 at 12:07
  • If two arrays have the same length, then both arrays should have the same position. For example, if two arrays have the largest length, they are number 1 – Bayu Jun 26 '12 at 12:14
  • Is this limited to 4 arrays? Do you have an array of arrays? How do you want the result to be formated? (array, collection). Assigning each array to a variable contradicts what you are saying in the previous comment. Get your thoughts together and ask properly. – Angel Jun 26 '12 at 12:18
  • @Angel: I'm sorry for being unclear. Assigning each array to a variable is just one example of the result might be. My answers: Yes this is limited to 4 arrays. I don't have array of arrays. To make it easier, I want to create function that can return me an array with the second largest length. Hopefully the following example will make it clear: http://jsfiddle.net/WPRYf/ – Bayu Jun 26 '12 at 12:40
  • Ok, was a little late, I see one answer by Yoshi that might help you with what you need. – Angel Jun 26 '12 at 14:08
  • @Angel: Yes it is. Thanks for your replies – Bayu Jun 27 '12 at 04:32

2 Answers2

12
var
  a = ["a", "a"];
  b = ["b", "b", "b", "b"],
  c = ["c", "c", "c"],
  d = ["d"],
  container = [a, b, c, d];

​container.sort(function (a, b) {
  return b.length - a.length;
});

console.log(container);

container will be sorted from longest (most elements) to shortest (least number of elements). Access it as:

container[0] // longest
container[1] // 2nd longest
// ...

http://jsfiddle.net/pSRVq/

Yoshi
  • 54,081
  • 14
  • 89
  • 103
1

You can find the length of the array using the length method and then sort these according to their values to find the largest, second largest array.

heretolearn
  • 6,387
  • 4
  • 30
  • 53