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

Ireneusz Kopta
Ireneusz Kopta
6,212 Points

Problem

NumArray.sort(function(a-b){ return a-b; }); lessons about arrays method - part 2. How exactly this function works? Cant understand how this function sort numbers in order. if I got array of numbers NumArray=[6,2,7,1,3,7]; How that function put them in order.In my mind if you subtract 2 from 6 you got 4.and so on That its obvious But how does it arrange numbers in order after subtraction?

3 Answers

But a can be assigned the higher of the two numbers as well. The sort method could call the function with 5 and 4 for a and b respectively. So in this case a has been assigned the higher of the two numbers. Maybe I'm missing what you're saying.

It's the return value of the function which tells the sort method which element should come first. So in this case it would be return 5 - 4; which is a positive value. This tells the sort method that the second argument it passed into the function should actually come before the first argument it passed in.

it might help to write out the compare function in what I would call long form.

the short form:

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

the longer form:

function (a, b) {
    if (a < b) {
       return -1; // return some negative number, any negative number.  This tells the sort method that a should come before b.
    }
    if (a > b) {
       return 1; // return some positive number, any positive number.  This tells the sort method that b should come before a.
    }

    return 0; // They must be equal and they should remain unchanged with respect to themselves but sorted with respect to the other elements
}

Both of these functions will do the exact same thing. I'd much rather write the first one though. The return statements in the second function do not use the variables a and b at all.

So it's not that because a is first in the return statement that makes it sort in ascending order it's just that it needs to be first to make that calculation return the proper positive, negative, and zero values.

Adam Moore
Adam Moore
21,956 Points

Yeah, I wasn't sure how exactly it worked, so thanks! :)

You're welcome.

Adam Moore
Adam Moore
21,956 Points

There is no subtraction. In the "function(a,b)", "a" is assigned the lowest value in the array, and "b" is assigned the highest value in the array. Then the "return a-b" instructs to organize from "a" (lowest value) to "b" (highest value). If you were to switch the "return" to "return b-a", then it would switch "b" (highest value) and "a" (lowest value), and sort them from highest to lowest. At least I'm pretty sure that's how it works.

Hi Christopher,

The sort method will pass two elements from the array into the compare function. Not necessarily the highest and lowest number. It does this in order to determine which one should come first.

The sort method has no knowledge of which is lowest and which is highest when it is first starting out so it couldn't assign the lowest to a and the highest to b It's only going to know which is lowest and highest after the sorting is completed.

Adam Moore
Adam Moore
21,956 Points

Well, yeah, I figured that it didn't already know which one was the lowest and highest, but as far as the sorting of it goes, the "a" and "b" are assigned whichever happen to be the lowest and highest, respectively; which is why it depends on which of those variables comes first in the "return" statement.

But I do get what you are saying, so thanks for clarifying! :)