Can anyone recommend an article explaining how this works. I understand how array.sort() works but the array.sort(a bunch of stuff) has me confused. I know what it does in this example but I’d like an article explaining how

Thanks!
I understand a bit better now but I’m still having trouble internalizing it. I decided to go back and review, I might have been moving forward a bit too quickly

It has to do with how JS’s Array.sort() determines which value is greater (in subtraction, if a - b is negative, then b is greater). The sort method on its own will always put numbers that start with 1 before numbers that start with 2. That results in an improper sort.

We want to sort by number of teeth, which means sorting the whole array, not each object in the array. Scroll up to 9 Oct to see the solution given for comparison. Read down from there to see how the sort function is implemented.

Just for my own learning process, can you please tell me if I understood what the code below does, based on my comments explaining the functionality?

// This object array provides a different species of animals with their number of teeth as key-value pairs and the array will be used as an argument for the 'sortSpeciesByTeeth' function
const speciesArray = [ {speciesName:'shark', numTeeth:50}, {speciesName:'dog', numTeeth:42}, {speciesName:'alligator', numTeeth:80}, {speciesName:'human', numTeeth:32}];
// Write your code here:
// This function sorts the object array in ascending order based on the number of teeth each species possesses
const sortSpeciesByTeeth = speciesArr => {
// This statement uses the sort() method on the species array to sort the key-value pairs in ascending order by having declared 'animal1' and 'animal2' as 2 sub-arguments. These take 2 key-value pairs at a time to subtract the number and cause the ascension based on the subtraction's result.
return speciesArr.sort((animal1, animal2) => animal1.numTeeth - animal2.numTeeth)
}
// This function call takes the 'speciesArray' as an argument to be passed to the function above
console.log(sortSpeciesByTeeth(speciesArray))
// Should print:
// [ { speciesName: 'human', numTeeth: 32 },
// { speciesName: 'dog', numTeeth: 42 },
// { speciesName: 'shark', numTeeth: 50 },
// { speciesName: 'alligator', numTeeth: 80 } ]

Thank you very much! I struggle to understand the body of code from after the built-in method we are using in these exercises.

This is correct in that sort iterates over the entire array by comparing two elements at a time. Specifically, the Compare function returns the result of animal1.numTeeth - animal2.numTeeth.

The important part to consider is how sort decides the correct order of the two elements being compared. From the MDN documentation, it uses the following logic:
If Compare function return value > 0, this means animal2.numTeeth is less than animal1.numTeeth so sort animal2 before animal1
If Compare function return value < 0, this means animal1.numTeeth is less than animal2.numTeeth so sort animal1 before animal2
If Compare function return value === 0, this means animal1.numTeeth is equal to animal2.numTeeth so leave the current order unchanged.

Can someone explain to me why this sorts in ascending order instead of descending? The way I read it, the sort method would return the elements ordered in a fashion where speciesOne > speciesTwo, so that is descending?

Hi all. Using the sort method to solve this problem and I am still getting the original array recreated as an output. If I return 1 instead of return -1 (in my sort method), I get the array in ascending order so it seems that the code is working but just not the other way. Could be wrong. Gist and output below:

I realized that didn’t make sense after your reply so I re-did the code. Gist below. The 2 speciesObj are to replace the “a” and “b” used in the sort method examples (from the hint). Based on my understanding, a - b will give you ascending order but I was getting the original array back. I recalled that you said in a previous reply for a different problem that there has to be a return or console.log after curly braces and I added that for the correct result to output. @mtf you are really helping me understand all this and I can’t thank you enough.