Need a bit of a help with this code

I have this code :
“const sortSpeciesByTeeth = arr => arr.sort((speciesObj1, speciesObj2) => speciesObj1.numTeeth > speciesObj2.numTeeth)”

I had some similar ones always given in solution because I could not figoure out the code for the exercise. But they never explain the given code just basically.

So this supposed to be an arrow function… But what is the second arrow? I cant figoure it out and I think this is the key to understand. I can’t really go on now. Maybe the first arrow is what I can’t figoure out… because there are 2 parameters but still I’m lost.

Can somebody explain it to me? Or just put me in the right direction where to find it or where can I read about this? Thanks!!

I believe is something easy im missing. Please someone?

The arr.sort method takes a comparison function as an argument.

This, in turn, is nested in the sortSpeciesByTeeth function.

You can read more about the sort methon and it’s compare function here.

1 Like

but what syntax is with the 2 arrows?

You have a function within a function, both of which have been defined using arrow syntax.

function one(argument) {
  argument.method(function two(arg1, arg2) {
    return something you've done with arg1 & arg2;
  })
}
const one = argument =>
argument.method((arg1, arg2) =>
something you've done with arg1 & arg2)

Two ways of writing the same thing

1 Like

So it’s a function in a function? Do you have a lesson in mind where I can refresh my knowledge on this? Because its a pain in the as* for me now…

Skimming over the syllabus I can’t see a specific lesson covering this but it may likely be covered in one of them. Array methods are certainly covered in the array lessons.

Try to break it down into chunks.

You have an outer function. When you call that it runs all the code inside it so it gives you a quick way to call on a long process multiple times.

On this occasion your function takes one argument, an array (arr). You can call the function multiple times and give it a different array to work on.

The body of your outer function (eg the code it runs) takes the array you have given it and calls the sort method on it. Methods often take arguments to tell them what you want to do.

The sort method takes an optional function as an argument which tells the method what order you want to sort the array element in.

You have created an anonymous function for the sort method argument and in turn, given it two arguments which are both objects. I the body of this function you are asking the function to make the comparison, in this case if obj1.teeth is bigger than obj2.teeth.

I don’t know if this will help or confuse you more.

In summary you have a function which is the argument of a method. All of that is wrapped in another function.

If you want to research some articles try googling callback functions, nested functions and methods.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.