So I have an array with a bunch of information about cars that I am supposed to be sorted ascending by the car model.
var inventory = [
{ id: 1, car_make: "Lincoln", car_model: "Navigator", car_year: 2009 },
{ id: 2, car_make: "Mazda", car_model: "Miata MX-5", car_year: 2001 },
{ id: 3, car_make: "Land Rover", car_model: "Defender Ice Edition", car_year: 2010 },
{ id: 4, car_make: "Honda", car_model: "Accord", car_year: 1983 }
and this was the solution that was determined to solve it.
function sortCarInventory(inventory) {
return inventory.sort((a,b) => (a.car_model > b.car_model ? 1 : -1));
}
I get that its calling the array and parsing through it to sort them and that the parameters its doing this with are "a, b".
What I don't get is where are these parameters being passed from to start with. In addition when its saying "a.car_model" is this dot notation? And lastly why does using a conditional operator with the assigned values 1 or - 1 allow it to be sorted here.
Edit I read MDN docs and still didn't really understand why this works hence the question.