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 the array in length order

Hello,

I'm doing a code challenge but I can't find the answer ... :( Can you help me ?

"Challenge task 2 of 2 Sort the 'saying2' array, on about line 19, so that the words are listed in length order. The shortest first. Use the 'length' property on the strings in the sort function."

My code is that

<script>
      var saying1 = ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"];
      var saying2 = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog's", "back"];
      saying1.reverse();
      saying2.sort(function (a, b) {
      return saying2.length
      a > b;

      });
    </script>

I really don't understand why it's not sorted by length, can you tell me where I'm wrong ?

Thank you

5 Answers

Hi Mathieu,

You have to subtract the lengths of each string.

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

The compare function needs to return a positive, negative, or zero value to let the sort method know what order a and b should be in.

There's more info on this mdn page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Harry James
Harry James
14,780 Points

Was looking for a site that would better explain sorting. Thanks for that!

Hi Harry,

You're welcome. That mdn page contains a good description of the compare function and shows the longer general form of it.

This particular compare function written in the general form would look like:

function (a, b) {
    if (a.length < b.length) {
        return -1; // Any negative value, could be -87 and it will make no difference to the sort method
    }
    if (a.length > b.length) {
        return 1; // Any positive value
    }
    return 0; // They must be equal
}

This general form and the shorter expression I gave for an answer are functionally equivalent as far as the sort method is concerned. It will not be able to tell the difference between the two versions. It may be useful to you to plug in various numbers for a and b into both versions to see how they're functionally equivalent as it relates to the sort method.

You can always fall back on this general form if you're unable to come up with a shorter expression that does the same thing.

Harry James
Harry James
14,780 Points

I understand how it works partially as in If function(a,b) is less than 0, sort a to a lower value than b, i.e. a comes first. If function(a,b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. If function(a,b) is greater than 0, sort b to a lower index than a, i.e b comes first.

I just didn't understand why taking the two numbers away from each other decides whether a goes first or b goes first. How would a value of -1 for example, decide that a comes first. Or how does a returned value of 1 cause b to go first?

I may be going a bit off-topic here but, I'd appreciate if anybody knew the answer just for my personal sake. Haha!

Alexis Gourdol
Alexis Gourdol
8,467 Points

the link helps a lot to understand the compareFunction concept, thanks!

Harry James

Are you asking about how the sort method uses the the return value to do the actual sorting? Or just wondering why it has to return -1 to indicate that a should come before b?

Amanda Kuek
Amanda Kuek
4,109 Points

Hi Mathieu,

How's this?

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

Cheers, Amanda.

Hello Amanda,

Thank you for your answer. Unfortunatly it's not working :/ I'll keep looking to figure this out

Thank you

Amanda Kuek
Amanda Kuek
4,109 Points

Sorry, the one I said previously sorts in descending (longest first). Try reversing?

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

Hi Amanda,

It's risky returning a boolean from the compare function and it's not always guaranteed to work.

Amanda / Jason, Thank you for your answers :)