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

I stuck on code challenge-Array sort method

Hi guys I'm stuck on task number 2 can't figure out the right format for the function to list the Array in length order using the "length" property http://teamtreehouse.com/library/websites/javascript-foundations/arrays/methods-part-2-4 need help.

2 Answers

This MDN page for the sort() method gives a good description and framework for the function. It definitely helped me out. Just read the top part of the description and base your function on the 'compare' function shown.

So, in this example, your function just needs to be placed within the parentheses of the sort() method, and must tell the method to sort by each array item's .length property. The first part of your conditional could be:

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

With this, if a is shorter than b, it will be placed before b. Two more conditions should be added to handle when a is longer than b, and when they are the same length.

Hope this helps!!

Thanks Paul!!

Your definition and link helped me wrap my brain around it even better. Seeing it written out as "if... if..." helps me grasp the short-hand used in the video.

Glad it helped! Mozilla Developer Network is an amazing resource and the explanations are always extremely helpful.

I haven't watched the video yet myself. I just discovered Treehouse today and am looking to brush up on my javascript and move forward from there!

Thanks Paul Welcome to treehouse And good luck

Hi Hossam, I'm going to get wordy in this reply for my own benefit. I'm trying to wrap my brain around JavaScript, too. And if I can explain this, it'll help me understand it.

So if we just said

saying2.sort()

the little man-in-the-machine wouldn't know how we want the values in our saying2 array sorted. We want them sorted by length. So we have to give it a function that compares the strings in our array and sorts them by length.

From the pop-up in the video @ 3:36: myArray.sort ( compareFunction ) sorts an array by using the compareFunction to determine the order.

The function we're going to give it will be

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

What will this function do? 'a' & 'b' are just variables within this anonymous function. We could've called them anything we want. If given values for these variables 'a' & 'b', it'll compare the length of 'a' to the length of 'b' by subtracting b.length from a.length .

So what, right?

Well when we put it altogether into our request to sort our saying2 array:

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

it knows that we're using this function as a means to compare each value in saying2 and sort saying2 . Voila!

Taking two values from saying2 at a time, and plugging them in for 'a' & 'b', it'll compare them and return a.length - b.length . If a negative number is returned, 'a' must come before 'b' in our newly sorted saying2 array. If the function returns a positive number, then 'a' comes after 'b'. And if zero is returned, then they could go in either order.

I hope this is at least a little clear and not even more confusing.

Hi Gary That was very good answer Thank you so much for help