2

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.

  • The details of sort could be found with a simple search engine search. `sort()` has to evaluate elements against each other to know which is "larger" than the other. The two arguments given to it are each element it has to compare. You are giving it a callback to define how to compare them. – Taplar Dec 27 '19 at 23:42
  • I think if you [read the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) all of your questions will be answered. – ggorlen Dec 27 '19 at 23:43
  • 2
    Side note; since this logic is doing string comparisons, really it should look like `(a, b) => a.car_model.localeCompare(b.car_model))` – Taplar Dec 27 '19 at 23:43

1 Answers1

3

The most important thing to understand is, that you are passing function as parameter. You can write it also as this to show what is actually being passed to .sort (I also wrote it in more descriptive version to see even for beginners whats happening there).

inventory.sort(compareStrings);
function compareStrings(a, b) {
   if (a.car_model > b.car_model) {
     return 1;
   } else {
     return -1;
   }
}

This means that .sort expects one parameter and that parameter is function. You are not the one who execute this function, the .sort method does it. Therefore it is the .sort method that is populating parameters a, b.

To work properly the function you provide needs to handle two parameters and you need to return 1 if first parameter is bigger or -1 if second parameter is bigger. (also you should return 0 if it is equal, which is something that can be improved in code provided as resolver)

How the .sort calling this function is blackbox to you.

note: javascript language allows to use > even for strings and it returns true/false based on lexicographical value.

libik
  • 22,239
  • 9
  • 44
  • 87
  • 2
    "*To work properly the function you provide needs to handle two parameters and you need to return 1 if first parameter is bigger or -1 if second parameter is bigger.*" it also needs to return `0` if the two are equal. Without doing that, you might get inconsistent results in some cases. The cases will depend on environment and the array being sorted. – VLAZ Dec 28 '19 at 00:05