Sort an Array Alphabetically using the sort Method 5

Tell us what’s happening:
I’m incredibly stuck on this one. Have tried using the hints to get help, but nothing has helped me. I cannot figure out what in the world it is that I am doing wrong with this one, now. My air-headed brain is going crazy today with coding.

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
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 Avast/72.0.1174.122.

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

Sorry for the late reply, sir. I have been busy with school yet again. But, I checked over the article, and it makes no sense to me, as my learning disability keeps kicking in. So, do you have any examples to how it’s done? Maybe an example or explanation would help me.

Sort functions take two arguments (elements to compare) and you need to return 1, 0 or -1 depending on your sorting logic.
• return 1 if first argument has higher priority
• return 0 if they’re equal
• return -1 if first argument has lower priority

The website never helped me. Sorry. I also haven’t been on much because of school taking up so much of my time right now.

How would I do that? Sorry, I have a learning disability and can’t understand things easily.

You will need to start familiarising with it, if you want to go the developer way, as it is where you would go to search for new methods to use or how to implement things.
Maybe try with the pages of things you already know how to use?

I haven’t been on here for like, a week now, and I forgot everything I had to do, all because I’ve been so busy with school and all of that. So I don’t know where to start and where to stop at. I just go with the flow and never skip lessons.

I actually just went over this lesson myself so I’m not an expert but hopefully I can help make it simple. So basically, the part with letters makes no sense and I don’t understand why it’s even there. If you plug this (which is in the tutorial example) in:

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a < b;
  });
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);

If you copy and paste this function exactly how it is provided in the example, it doesn’t work! I tried looking it up and I don’t understand why this example exists with information that doesn’t work. So basically, this is what you need to know:

  1. arr.sort() returns the letters in an array alphabetical order.
  2. arr.sort().reverse() returns the letters in reverse alphabetical order because it sorts it alphabetically first with sort() and then reverses it with reverse().
  3. If you want to sort numbers, you have to use a function in the parenthesis that returns a negative, 0, or positive. This is called a compare function. It compares two values of the array at a time and sorts them by the guidelines you give.

thanks for clearing that up.

The MDN documentation referenced above never helped me at all. I don’t understand it at all, and it’s hard to understand with a learning disability.

@LBDemaree

It can be frustrating. Reading MDN documentation is indeed challenging even for experienced developers. However, you need to build resistance towards it and really strive to digest its information at least partially. Read the code line by line and really try to make sense out of it. I am sure if you build this foundation, you can be a good developer.

With that said, what part of the documentation doesn’t make sense to you?

Everything about the documentation doesn’t make sense to me. I have read it over and over again, and I still don’t understand it.

It is a bit vague…
The articles usually have an introduction, followed by the description of the syntax, then the description on how the method works, examples which usually also offer extra infos for specific implementations, and then specifications and browser compatibility
It can be a bit technical sometimes if you don’t have a background on certain things, but it can be a push to research new things.
There are also interactive examples that you can try, you can try to play with a method till you understand what’s explained there…

The documentation is the only place where you have all the methods described one by one, if you don’t use it it would be like trying to learn a new language without a dictionary

If it is the way that the MDN is presented, google things, you will find results in many different places, like stackoverflow, various website and blogs and articles…
Anyway, try if you have the same issue with https://www.w3schools.com/

I’m using that website, but, I’m still not understanding. My code that I put in is this:

function alphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a,b) {
return a < b;
});

// Add your code above this line
}
alphabeticalOrder([“a”, “d”, “c”, “a”, “z”, “g”]);

What am I doing wrong here?

As per the documentation, the callback:

  • Should return -1 when a < b
  • Should return 0 when a and b are equal
  • Should return 1 when a > b

Your callback

  • Returns true if a < b
  • Returns false otherwise
1 Like

The MDN Web Docs do not help me. Someone has already tried those with me, and I do not understand what they say at all.

All of it. None of it makes sense to me, @camperextraordinaire. My brain doesn’t comprehend things easily as my learning disability is always kicking in.

Now you aren’t making any sense. I’m sorry, but I’m having trouble understanding everything today as I’m very sick.

Okay, ignore the documentation, but to repeat what has been said a few time here, with your code, you have written this:

function(a,b) {
  return a < b;
}

Sort compares two values at a time. Your code returns true if a is less than b, false otherwise.

This is not what sort needs to be able to work in this case, the function is supposed to return -1, 0 or 1.

But as @camperextraordinaire says, you need to try to learn to to read documentation, this is really important, it is not possible to successfully develop very much in the way of software when you can’t do this. There has to be something (or several things) on that page that you specifically don’t get

It seems impossible with y’all to learn on this when I don’t understand a whole lot of things. I have been trying to learn code for school since December, and up until now, I am getting nowhere with help. I keep trying and trying and trying by myself to complete these, but no one is helping me anymore. Makes me think why I’m a member of this website anymore now because no one is wanting to really help someone with a learning disability.