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

Sort function

How can I implement the length property in the sort function?

4 Answers

Matthew Smart
Matthew Smart
12,567 Points

what do you mean by this? can you provide more in depth description of what your trying to do?

When I do the exercise 2 of method parts 2 of js, I must utilise the function sort and in this function I should the length property to sort an array.

Sreng Hong
Sreng Hong
15,083 Points

Hi Piero!!! In exercise 2, it's asked you to sort the word length in order.

So you have to use method array.sort(sortfunction) and write your own function to solve the right problem

The code should be like this:

saying2.sort(function([parameters]){
        //your code
});

Like Sreng Hong pointed out, you'll need to write your own code, but it involves comparing the lengths of any given string within the array. It takes a little more brainpower than the web design exercises, but you can do it.