Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.