diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/index.md index d96f2be5737..12eb8d3e02d 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/index.md @@ -3,35 +3,38 @@ title: Sort an Array Alphabetically using the sort Method --- ## Sort an Array Alphabetically using the sort Method -### Method +Hint #1 -In the example given we see how to write a function which will return a new array in reverse alphabetical order. +You need to use a "compare function" as the callback function of the sort method. -```javascript +For example, the following is how you would sort an array in reverse alphabetical order. -function reverseAlpha(arr) { - return arr.sort(function(a, b) { - return a !== b ? a > b ? -1 : 1 : 0; +```js +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 } -reverseAlpha(['l', 'h', 'z', 'b', 's']); +reverseAlphabeticalOrder(['l', 'h', 'z', 'b', 's']); // Returns ['z', 's', 'l', 'h', 'b'] - ``` -Using this logic, simply reverse engineer the function to return a new array in alphabetical order. +### Solution #1 -### Solution - -```javascript +
+Spoiler Alert - Only click here to see the solution +```js function alphabeticalOrder(arr) { // Add your code below this line return arr.sort(function(a,b) { - return a !== b ? a < b ? -1 : 1 : 0; + return a === b ? 0 : a < b ? -1 : 1; }); // Add your code above this line } alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); - ``` + +