1

I'm following Free Code Camp's "JavaScript Algorithms and Data Structures" tutorial.

I have a challenge and a hint and solution to this challenge, but I don't understand the solution, neither the hint.

Here is the challenge:

Use the sort method in the alphabetical Order function to sort the elements of arr in alphabetical order.

Here is the hint:

Hint #1

You need to use a “compare function” as the callback function of the sort method.

For example, the following is how you would sort an array in reverse alphabetical order.

function reverseAlphabeticalOrder(arr) {
  return arr.sort(function(a, b) {
    return a === b ? 0 : a < b ? 1 : -1;
  });
}
reverseAlphabeticalOrder(["l", "h", "z", "b", "s"]);

Here is the solution:

Solution #1

function alphabeticalOrder(arr) {
  return arr.sort(function(a, b) {
    return a === b ? 0 : a < b ? -1 : 1;
  });
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

Here is the link to this challenge

Could you please explain the solution. Once I'll get the solution explained, I will be able to understand the hint too.

Thank you very much in advance for your help.

Best regards, Helena

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 3
    Take a look at the MDN documentation for [`Array.prototype.sort()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Kobe Jan 30 '20 at 13:13
  • 1
    once you've read the docs above, edit your question so it can be more specific – Fabrizio Calderan Jan 30 '20 at 13:15
  • Hello, 04FS. I don't understand this line "return a === b ? 0 : a < b ? -1 : 1;" Thank you in advance. – Helena Code Jan 30 '20 at 13:16
  • it's a ternary operator containing another nested ternary operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – Fabrizio Calderan Jan 30 '20 at 13:17
  • 3
    1. [How does Javascript's sort() work?](https://stackoverflow.com/questions/1494713/how-does-javascripts-sort-work) 2. [How to sort strings in JavaScript](https://stackoverflow.com/questions/51165/how-to-sort-strings-in-javascript) 3. [Question mark and colon in JavaScript](https://stackoverflow.com/questions/1771786/question-mark-and-colon-in-javascript) 4. [How do you use the ? : (conditional) operator in JavaScript?](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript) – VLAZ Jan 30 '20 at 13:17

1 Answers1

-1

If you want to understand return a === b ? 0 : a < b ? -1 : 1; then, this is equivalent to:

if( a === b ){
   return 0;
}else if( a < b ){
   return -1;
}else{
   return 1;
} 
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
  • 1
    Thank you, Sudhir Ojha, Now I understand better the second part of explanation from FCC:" If compareFunction(a,b) returns a value less than 0 for two elements a and b, then a will come before b. If compareFunction(a,b) returns a value greater than 0 for two elements a and b, then b will come before a. If compareFunction(a,b) returns a value equal to 0 for two elements a and b, then a and b will remain unchanged." https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method – Helena Code Jan 30 '20 at 15:33