Reverse - Why doesn't .sort() work?

Hi, I was wondering why the .sort(reverse = True) doesn’t work for the Reverse problem???
From what I understand it only works for lists. But if you split a string to make it into a list, use .sort and then .join the list to make it into a string again. How come that doesn’t work???

This is the problem.
"Write a function named reverse_string that has a string named word as a parameter. The function should return word in reverse. "

sounds like it could work. Could you provide:

  1. your code
  2. the exercise url/link
def reverse_string(word):
  new_word = word.split()
  new_word.sort(reverse= True)
  "".join(new_word)
  return new_word

I actually just rewrote it. I had already moved on to the range/len method.
So now this code is giving me;

[‘Codecademy’]
[‘world!’, ‘Hello’]

Whilst before it gave me SyntaxError = something to do with “does not work on string”. Anyway, this code would be similar to what I wrote before. :sweat_smile:

.join() returns the new joined/glued string. But you do nothing with this returned result.

1 Like

What do you mean, I do nothing with the return result? Isn’t join() the final step?

I’ve just notice something. The sort is not doing anything.
I printed every stage to see properly.

def reverse_string(word):
  print(word)
  new_word = word.split()
  print(new_word)
  **print(new_word.sort(reverse=True))**
  print("".join(new_word))
  return new_word

This is what I get.

Codecademy
[‘Codecademy’]
None
Codecademy
[‘Codecademy’]
Hello world!
[‘Hello’, ‘world!’]
None
world!Hello
[‘world!’, ‘Hello’]


None

Am I writing it wrong?

There’s a few possible issues here so I’ve listed them out; apologies if not all of them fit but worth checking them to see if they match what you expect.

I think you might be confusing the operation of some of these methods as things like .sort() work in-place and modify the existing list, they don’t have a valid return so your print just shows the None object. Other methods create new objects and do have valid returns such as the .join() method you used.

The second thing that might be tripping you up here is what .sort() does. It only sorts the elements of a list, if your list has a single element then there will be no change to the list; it also sorts by the ascii value of the characters not by the order of items in the string list.

A third issue might might unfortunately stem from a confusion about what .split() does since it will not separate a single word into multiple characters like this.

Edit: Thought I should the add that the sort is really unicode based rather than just ascii, here’s a nice intro to how sorting a list of strings works if you wanted some details: https://realpython.com/python-sort/#when-youre-sorting-strings-case-matters

1 Like

Thank you for the explanations, tgrtim. I had to look up ascii and code point :grin:
So, ok. The reality is, that I can’t really use sort for just one string. There needs to be a number of items in a list in order for it to work. Like we see with “Hello World” at the end. That’s why the hint tells us to use the range/len method, instead. Of course!!!

Thanks again :slightly_smiling_face:

1 Like

There are other ways to solve this task but some of the lessons are trying to reinforce the things you’re learning so they prefer the use of some of the “lower” level tools like looping and using range with whatever you’ve covered recently.

Especially if you’re just starting out solving tasks like this is much better practice than what might be an “easier” solution.

1 Like