Sort an Array Alphabetically using the sort Method!

Sort an Array Alphabetically using the sort Method!
0

When I run the following code in node.js, I get the results that are given. But when I run the same code in this browser I get error messages about what the output should be. Whats going on

Your code so far


function alphabeticalOrder(arr) {
   // Add your code below this line
     return arr.sort(function(a, b) {
     return a >= b;
   });
   
   // Add your code above this line
 }
 
 console.log(alphabeticalOrder(["a", "d", "c", "a", "z", "g"]));// ["a", "a", "c", "d", "g", "z"].
 console.log(alphabeticalOrder(["x", "h", "a", "m", "n", "m"]));// ["a", "h", "m", "m", "n", "x"].
 console.log(alphabeticalOrder(["a", "a", "a", "a", "x", "t"]));//  ["a", "a", "a", "a", "t", "x"].

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36.

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

The sort function should not return a boolean.

I don’t see any error messages. Do you mean the tests aren’t passing?

The tests aren’t passing because the sort callback isn’t sorting correctly.

From the link in @ArielLeslie’s post:

If compareFunction is supplied, all non-undefined array elements are sorted according to the return value of the compare function. … If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to an index lower than b (i.e. a comes first).
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. …
  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a (i.e. b comes first).

Your sort callback doesn’t ever return 0, 1, or -1. It uses the >= operator, which only ever returns true or false (which coerce to 1 and 0 respectively, never -1).

The interesting thing is the challenge can be passed without using a compareFunction based on what is being sorted and tested.

Ahh, but then you miss out on learning how the sort callback works! :stuck_out_tongue:

I believe not using the compare function may not yield a stable sort, but don’t hold me to that.

Per the spec, I don’t think it’s ever required to be stable, regardless of whether a callback is used. Though it looks like it is at least stable in Chrome 70+.

1 Like

You would think that would be a requirement in the ECMA standard.

wow I seem to have spawned a lengthy discussion :slight_smile:

The code Ive written gives the right result when I run it in node.js, but not when I run it on the website. It says that the results should be those given at the bottom of the left side.

So, its not that the code isnt working, I just dont know why Im getting different result on the site from those using node.js

Can anyone explain

Node’s sort implementation is different than Chrome’s, so that is more than likely the difference. Definitely take the opportunity to read about using compareFunctions as you can use them to solve this challenge plus learn something very valuable for other challenges later. The compare function will need to return a -1, 0, or 1.

okay, I mean I understand about the +1, 0, -1 but I was just wondering that if someone is programming with js using visual code and node.js, then how can they be sure that their code is production ready? And then what if the code work in chrome, but the user is using firefox or opera or something. Then if the code doesnt work, the user will think its broken. Things that make you go …hmmmmmm

Hmm, very curious. Based on this Reddit thread, it looks like that’s due to Chrome changing the algorithm from QuickSort (unstable) to TimSort (stable), the latter of which Node 11+ also uses. You can check which version of Node you’re using with node --version.

I’m actually surprised this change was ever made, given that the usual philosophy with JavaScript is “never change the old behavior (even if it’s plain incorrect) for fear of breaking legacy code”. I guess they reviewed actual usage and found that very few sites were relying on the old behavior.

     return arr.sort((a, b) => 
     {
     return (a>b)?1:(a==b)?0:-1;
     });

it works but Im puzzled about the other way, why it wont work here but does in node.js

If you use a compareFunction it is guaranteed to work across all browsers. Chrome made a change which almost forces you to use compareFunctions. The other browsers are like Chrome used to be, so they are a little more flexible.

1 Like