fix: added better hint and solution (#35746)

This commit is contained in:
Randell Dawson
2019-05-01 15:09:45 -07:00
committed by Manish Giri
parent a43d6c6daf
commit 88e7c2d704

View File

@ -3,35 +3,38 @@ title: Sort an Array Alphabetically using the sort Method
--- ---
## 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) { ```js
function reverseAlphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a,b) { 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
} }
reverseAlpha(['l', 'h', 'z', 'b', 's']); reverseAlphabeticalOrder(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b'] // 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 <details>
<summary>Spoiler Alert - Only click here to see the solution</summary>
```javascript
```js
function alphabeticalOrder(arr) { function alphabeticalOrder(arr) {
// Add your code below this line // Add your code below this line
return arr.sort(function(a,b) { 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 // Add your code above this line
} }
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]); alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
``` ```
</details>