1

I have this array of arrays. I want it to be sorted by arrays' length. I use array.sort( (a, b) => a.length > b.length) );

[ 
  [],
  [ 2, 2, 2, 2, 2 ],
  [],
  [],
  [ 5 ],
  [ 3, 3, 3, 3 ],
  [],
  [],
  [ 1 ],
  [ 2, 2 ],
  [ 1 ],
  [],
  [ 4 ],
  [ 3 ],
  [ 1, 1 ],
  [],
  [ 5, 5 ],
  [],
  [],
  [ 4, 4, 4, 4, 4, 4 ],
  [],
  [ 3 ],
  [ 5 ],
  [],
  [ 5 ],
  [ 1, 1 ],
  [],
  [ 3 ],
  [],
  [],
  [],
  [],
  [ 4 ],
  [ 1 ],
  [ 4, 4, 4, 4 ],
  [],
  [ 5, 5, 5, 5 ],
  [],
  [],
  [ 3 ],
  [ 5 ],
  [ 2 ],
  [],
  [],
  [ 2 ],
  [],
  [],
  [ 1, 1, 1 ],
  [],
  [ 4 ],
  [ 3, 3, 3 ],
  [],
  [],
  [ 1, 1 ],
  [],
  [ 4, 4 ],
  [ 2, 2, 2 ],
  [],
  [ 2 ],
  [ 2 ],
  [ 4 ],
  [ 2 ],
  [ 3 ],
  [ 2 ],
  [],
  [],
  [],
  [ 5 ] ]

But after all sorting returns this. Why array of length of 6 with 4 inside it is placed in the wrong place?

[ 
  [],
  [],
  [],
  [],
  [ 5 ],
  [ 2 ],
  [],
  [],
  [ 1 ],
  [ 3 ],
  [ 1 ],
  [],
  [ 4 ],
  [ 3 ],
  [ 2 ],
  [],
  [ 4 ],
  [],
  [],
  [ 2 ],
  [],
  [ 3 ],
  [ 5 ],
  [],
  [ 5 ],
  [ 2 ],
  [],
  [ 2 ],
  [],
  [],
  [],
  [],
  [ 4 ],
  [ 1 ],
  [],
  [],
  [],
  [],
  [],
  [ 3 ],
  [ 5 ],
  [ 2 ],
  [],
  [],
  [ 2 ],
  [],
  [],
  [],
  [],
  [ 4 ],
  [],
  [ 4, 4, 4, 4, 4, 4 ],
  [ 5 ],
  [ 5, 5 ],
  [ 1, 1 ],
  [ 4, 4 ],
  [ 3, 3 ],
  [ 3, 3 ],
  [ 1, 1 ],
  [ 2, 2 ],
  [ 1, 1 ],
  [ 1, 1, 1 ],
  [ 2, 2, 2 ],
  [ 3, 3, 3 ],
  [ 5, 5, 5, 5 ],
  [ 3, 3, 3, 3 ],
  [ 4, 4, 4, 4 ],
  [ 2, 2, 2, 2, 2 ] ]

What is wrong with this?

2

2 Answers 2

2

To do the sort function, you have to provide the max, min equal (+1 (>0 actually), -1 (<0 actually), 0 (for equals)), the faster way is by substract the results:

arr.sort(function(a, b){
  // ASC  -> a.length - b.length
  // DESC -> b.length - a.length
  return a.length - b.length ;
});
Sign up to request clarification or add additional context in comments.

Comments

2

Just use the sort function using the subtraction between lengths:

  var a = [ 
  [],
  [ 2, 2, 2, 2, 2 ],
  [],
  [],
  [ 5 ],
  [ 3, 3, 3, 3 ],
  [],
  [],
  [ 1 ],
  [ 2, 2 ],
  [ 1 ],
  [],
  [ 4 ],
  [ 3 ],
  [ 1, 1 ],
  [],
  [ 5, 5 ],
  [],
  [],
  [ 4, 4, 4, 4, 4, 4 ],
  [],
  [ 3 ],
  [ 5 ],
  [],
  [ 5 ],
  [ 1, 1 ],
  [],
  [ 3 ],
  [],
  [],
  [],
  [],
  [ 4 ],
  [ 1 ],
  [ 4, 4, 4, 4 ],
  [],
  [ 5, 5, 5, 5 ],
  [],
  [],
  [ 3 ],
  [ 5 ],
  [ 2 ],
  [],
  [],
  [ 2 ],
  [],
  [],
  [ 1, 1, 1 ],
  [],
  [ 4 ],
  [ 3, 3, 3 ],
  [],
  [],
  [ 1, 1 ],
  [],
  [ 4, 4 ],
  [ 2, 2, 2 ],
  [],
  [ 2 ],
  [ 2 ],
  [ 4 ],
  [ 2 ],
  [ 3 ],
  [ 2 ],
  [],
  [],
  [],
  [ 5 ] ];
  a.sort(function(a, b) {
  return a.length - b.length;
  });
  console.log(a);

1 Comment

That's actually the same you posted so great job to you too :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.