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

Mike Staebell
Mike Staebell
6,177 Points

Sort array by string length

I'm having a tough time with the second task in this challenge. (Methods Pt 2)

I think the video should go into more detail about comparative functions. I've watched it a few times and still have a tough time wrapping my head around why passing in an arbitrary a,b as part of an anonymous function would allow you to customize how the sort method works. In my mind, I see two arguments passed in, yet there could be a number of values in an array. How the comparative function indexes through the entire array using these arbitrary arguments (a,b) is a concept that goes right over my head.

The instructor has done a great job so far, but in this instance I believe he glossed over a crucial concept for a js newb like me.

Thanks in advance for any guidance anyone can provide me regarding comparative functions and custom-sorting of arrays.

3 Answers

Hi Mike,

Around 3:45 in the video is where Jim explains what values the compare function needs to return.

From Array.prototype.sort():

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.

  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a.

That is what Jim is explaining around the 3:45 mark of the video.

In this video JIm is treating the .sort method as a black box. We don't know how it works but we know the requirements of the compare function. As long as we write a proper compare function, the sort method will properly sort the array for us.

It sounds like you want to know more about the sort process. The compare function doesn't index the entire array and isn't involved in the actual sorting process.

The sort method handles the actual sorting using a particular sorting algorithm

The sort method as it is going through it's algorithm will take 2 elements from the array and pass it to the compare function. The compare function then returns a positive, negative, or zero value which lets the sort method know the relative order of those 2 elements.

The sort method continues on passing different elements of the array to the compare function. Always 2 at a time until the array is finally sorted.

Let me know if it's making more sense to you and if you have anymore questions about this.

Mike Staebell
Mike Staebell
6,177 Points

Jason, that makes sense now that you explain it that way.

Thank you both!