I want to create a new array that is copy of mine array and at the same time sort it using the value stored in numTeeth. I'm using the function sort to accomplish this task. The problem is that if I try to debug it using a console log it show as result the undefined value.
const speciesArray = [
{speciesName:'shark', numTeeth:50},
{speciesName:'dog', numTeeth:42},
{speciesName:'alligator', numTeeth:80},
{speciesName:'human', numTeeth:32}
];
const sortSpeciesByTeeth = arrayIn => {
arrayIn.sort( function (a, b) {
return a.numTeeth - b.numTeeth;
});
}
console.log(sortSpeciesByTeeth(speciesArray))
While if I use the same code without declaring it as separate function it works despite it sort the original array. What I don't want in the final code. Example
const speciesArray = [
{speciesName:'shark', numTeeth:50},
{speciesName:'dog', numTeeth:42},
{speciesName:'alligator', numTeeth:80},
{speciesName:'human', numTeeth:32}
];
speciesArray.sort( function (a, b) {
return a.numTeeth - b.numTeeth;
});
console.log(speciesArray)