From 88e7c2d7043300dc156d60244e171b07c7903372 Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Wed, 1 May 2019 15:09:45 -0700 Subject: [PATCH] fix: added better hint and solution (#35746) --- .../index.md | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) 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 d96f2be573..12eb8d3e02 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"]); - ``` + +