0

So earlier I asked this question: How to sort list depending on values in sublists? I wanted to know how to sort some sublists, and I am using this solution:

example = (sorted(sorted(example), key=itemgetter(1), reverse=True))

But now I am noticing that this is not sorting the numbers quite correctly, e.g. 25, 43 23, 3 is being sorted as 43, 3, 25, 23. Obviously 3 is not greater than 25! So how do I change this to check for the whole number? Not just the first place holder.

Community
  • 1
  • 1
Hayley van Waas
  • 429
  • 3
  • 12
  • 21

1 Answers1

1

It looks like example is a list of strings, so you need to sort by the int of the values:

example.sort(key=int, reverse=True)

I'm not sure what the nested sorteds were for. I took those out.

I also changed sorted to sort, since you were reassigning to the same variable name. If the list is referenced elsewhere by a different name, you had the right approach. Otherwise this will have somewhat lower overhead.

mhlester
  • 22,781
  • 10
  • 52
  • 75