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 JavaScript Foundations Arrays Methods: Part 2

help with challenge 2 in methods part 2 why isn't this code right? it returns the correct order?

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

2 Answers

thank you very much-keep up the good work!

Dave McFarland
Dave McFarland
Treehouse Teacher

When you use a sorting function, the 2 arguments you pass to it represent 2 items in the array:

myArray.sort(function( a, b) {


a is one array item and b is another array item. To compare their lengths you just need to use the length property of each:

a.length - b.length