Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Edd Figueiredo
Edd Figueiredo
2,268 Points

Difficulty with the second challenge in Arrays section

Well, I'm having problems with the second task of the second challenge. It tells me to sort the 'saying2' array by the length of the objects beginning with the shortest. I used my function as follows :

saying2.sort( function (){ saying2.length});

And the result was the objects beginning by the shortest, but not in alphabetical order. I'm stuck...

2 Answers

Edd Figueiredo
Edd Figueiredo
2,268 Points

Hey, I found the solution the internet... I just need to pass the arguments in the function and subtract them by length. like:

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

Sander de Wijs
PLUS
Sander de Wijs
Courses Plus Student 22,267 Points

Thanks, I also needed help with that one. Guess the real challenge is keeping it simple :-) I was trying to get the lengt of each array item first.