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) {
// Add your code below this line
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? 1 : -1;
});
// Add your code above this line
}
reverseAlphabeticalOrder(["l", "h", "z", "b", "s"]);
// Returns ['z', 's', 'l', 'h', 'b']
Solutions
Solution 1 (Click to Show/Hide)
function alphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? -1 : 1;
});
// Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
Well, I think I figured it out. Mainly in a mathematics, let’s say we have a set of numbers ( 20, 10, 5, 25) and we want to rearrange them ascending, we pick the first number ( 20 ) and compare it to each other number in the set to check if it is the lowest number in the order criteria until we pick a number is lower than it to put it first in the order. If we didn’t find a lower number so ( 20 ) will be the first number in the order but, if we found a lower number, we replace the 20 with that number and start comparing it with other numbers and repeat this steps till finding the lowest ordered number then we move to the next one. For our example: we will check if 20 < 10 by subtracting them ( 20 - 10 ), and then we have 3 cases for the result:
1- if it is negative so 10 is lower than 20 so 20 replaced by 10 and we start comparing 10 with other numbers.
2- if it is positive so 20 is lower than 10 and it still the first temporary first ordered number.
3- if it is 0 so both are the same value and they remain in the same order. (( not sure that I have said this right ))
by repeating this steps for each number we can order them with specific criteria.
I think another explanation is b - a returns largest to smallest BECAUSE: in unicode a comes before b. Therefore a being 1st and b being 2nd. So in other words the array iterates from 2 (being the larger of the two numbers) to 1 (being the lowest) That’s the way that makes the most sens to me at least lol. Hope that helps in some way