fix: added better hint and solution (#35746)

pull/35801/head
Randell Dawson 2019-05-01 15:09:45 -07:00 committed by Manish Giri
parent a43d6c6daf
commit 88e7c2d704
1 changed files with 17 additions and 14 deletions

View File

@ -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
<details>
<summary>Spoiler Alert - Only click here to see the solution</summary>
```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"]);
```
</details>