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

Kyle Germaine
Kyle Germaine
8,174 Points

Arguments of functions - Javascript

Maybe I'm just slow, but I would have liked a more thorough lesson around the arguments in the below function and how they're loops operate. A more detailed explanation on how the array values are assigned and computed within the function, would be helpful.

var my_array = ["one", "two", "three", "four", "five"];

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

I've figured it out now through other resources, but thought that it was skipped over relatively quickly in the lesson compared to the majority of the section, which covered more basic terminology.

Regards, Kyle

P.S. I see that my next lesson is 'Functions' so maybe just a little bit of reorganization would help.

I agree. And I'm afraid to even admit how i was trying to get this to work. Fortunately, Kyle, you helped me out with this post. I was struggling with this and I think that it had to do with the fact that I was unclear how sort was implemented.

Kyle Germaine
Kyle Germaine
8,174 Points

Paul,

Glad my complaining could help! Andrew gives a pretty great explanation below.

4 Answers

Andrew Corcoran
Andrew Corcoran
20,552 Points

Hi Kyle,

I struggled trying to figure this out as well. To perform a numeric sort, you must pass a function as an argument when calling the sort method (as opposed to not passing a function for an alphabetical sort). The function defines the sort order or the numbers (ascending or descending) as shown below.

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

This works the way it does because whenever "a" is less than "b", a negative value is returned, which results in the smaller elements always appearing to the left of the larger ones, in other words, ascending. And the opposite is true of the below.

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

Andrew Corcoran
Andrew Corcoran
20,552 Points

I totally glazed over the part where you said you figured it out through other resources, sorry.

Kyle Germaine
Kyle Germaine
8,174 Points

Thanks Andrew, still a very helpful description!

I guess it was more of a suggestion/complaint than a question.

Regards. kyle

I'm stumped. Where does the anon function pick a and b from? Is it the first two elements in the array? I tried my_array.sort(-1); to see what happens if a-b returns a negative value, and the array was sorted as though the elements were strings again. Replacing -1 with 1 yields the same result, in other words, no change in the order. So a simple negative or positive value doesn't sort anything. The magic of the anon function escapes me.

Also, granted that the anon function sorts the two first elements, why does it also sort the the rest of the elements?