question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Answer for 'Functional Programming: Sort an Array Alphabetically using the sort Method' doesn't work in Chrome

See original GitHub issue

Describe your problem and how to reproduce it:

I was doing the Functional Programming: Sort an Array Alphabetically using the sort Method challenge, in which you’re supposed to sort an array of letters alphabetically. So I tried what I thought was the correct answer. It didn’t work. I looked at the answer and it was exactly the same but with a longer syntax:

return arr.sort((a,b)=> a>b));

I was very confused, I was so confident about that answer! So I started trying different things in codepen and jsfiddle, but that method wasn’t working at all:

// Chrome output
const arr = ["c", "a", "b"];
console.log(arr.sort((a,b)=>a>b)); //["c", "a", "b"]
console.log(arr.sort((a, b) => a.localeCompare(b))); //["a", "b", "c"]
console.log(arr.sort()); //["a", "b", "c"]

Still very confused I opened Firefox and did the same thing. But this time, all methods worked.

// Firefox output
const arr = ["c", "a", "b"];
console.log(arr.sort((a,b)=>a>b)); //["a", "b", "c"]
console.log(arr.sort((a, b) => a.localeCompare(b))); //["a", "b", "c"]
console.log(arr.sort()); //["a", "b", "c"]

It appears that the latest versions of Chrome won’t let you sort arrays alphabetically that way anymore. But I don’t know what would be the right way to tackle that problem.

Probably a good first step is to add a disclaimer to the solution guide explaining the issue.

What do you guys think?

– EDIT: It may be obvious, but I should mention that this is a problem for all the people using later versions of Chrome, because in both the challenge explanation and the solution, the method that is used to sort the array won’t work. So even if you copy and paste the solution, the tests will fail.

Add a Link to the page with the problem: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method

Tell us about your browser and operating system:

  • Browser Name: Chrome
  • Browser Version: 73.0.3683.86 (Official Build) (64-bit) –
  • Browser Name: Firefox
  • Browser Version: 67.0b8 (64-bit) –
  • Operating System: macOS Mojave 10.14.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RandellDawsoncommented, Apr 16, 2019

@guayom Though not reflected in the beta, the challenge was update`d on PR #35756. Also, on PR #35746, I have corrected the suggested solution and improved the hint example. I am just waiting for it to get merged.

0reactions
guayomcommented, Apr 16, 2019

@ojeytonwilliams @RandellDawson Oh I see now! That’s perfect! Thanks for your help! With all that said, I’ll close the issue since the changes will eventually get merged 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort an Array Alphabetically using the sort Method-not working
As @LiChoi says, just using the default with no callback function works (and this is actually what the hint has been changed to)....
Read more >
Sort an Array Alphabetically using the .sort() Method - YouTube
In this functional programming tutorial we sort an array alphabetically using the sort method. This video constitutes one part of many where ...
Read more >
Sorting Javascript Array with Chrome? - Stack Overflow
The comparer function should return a negative number, positive number, or zero (which is a convention across programming languages).
Read more >
JavaScript Array sort() Method - GeeksforGeeks
JavaScript Array.sort() Method is used to sort the array in place in a given order according to the compare() function. If the method...
Read more >
Order and limit data with Cloud Firestore | Firebase - Google
By default, a query retrieves all documents that satisfy the query in ascending order by document ID. You can specify the sort order...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found