-1

this is more than likely a duplicate question, however all my searches were unsuccessful in helping me. Anyways, I would like to sort this list from the largest number to the smallest number, yet it consistently prints None. Thanks in advance for the help!

list = ['1008', '1033', '1080', '3107', '3589', '574', '703', '704', '712', '731', '810', '857', '862', '909', '927', '980']

print(list.sort()) # Prints None
Bigc1109
  • 11
  • 3
  • `list.sort()` doesn't return anything you need to call `list.sort()` then print the list `print(list)` – deadshot Feb 26 '20 at 04:15
  • For string values **sort** consider ASCII value. Try this **list.sort(key=int)** – aleem md Feb 26 '20 at 04:16
  • Does this answer your question? [Why does "return list.sort()" return None, not the list?](https://stackoverflow.com/questions/7301110/why-does-return-list-sort-return-none-not-the-list) – AMC Feb 26 '20 at 04:27
  • 1
    Can you rephrase the title? It’s difficult to understand. Also, don’t name a variable `list`. – AMC Feb 26 '20 at 04:27
  • @AMC For the first question, that was one of the guides I found when trying to fix this issue and it didn't really work. Second, I did change the title, hopefully its a tad bit better, haha (I'm bad at wording things). Finally, this is just a small portion of a larger code, and is'nt actually named list in there. – Bigc1109 Feb 26 '20 at 04:35
  • @Bigc1109 What do you mean by _it didn’t really work_ ? – AMC Feb 26 '20 at 04:44
  • @aleemmd How do ASCII values relate to the rest of your comment, though? – AMC Feb 26 '20 at 04:48
  • @AMC The answer given on there only worked with the list already in integers. I didn't realize this was the issue with my code until I posted this. – Bigc1109 Feb 26 '20 at 04:48
  • @Bigc1109 Did you not know how to parse an int from a string? Either way, I think this is a duplicate. – AMC Feb 26 '20 at 04:50
  • @AMC I do know how to change a string into an integer, but as I said before, I didn't know that was the issue. – Bigc1109 Feb 26 '20 at 04:53
  • 2
    Does this answer your question? [How to sort a list of strings numerically?](https://stackoverflow.com/questions/3426108/how-to-sort-a-list-of-strings-numerically) – Gino Mempin Feb 26 '20 at 05:01

4 Answers4

0

It is doing inline sort. You have to try either of below approaches.

Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.

list.sort()
print(list)

or

print(sorted(list))

Your List is of character strings. First you have to convert them to integers and then sort.

>>> list
['1008', '1033', '1080', '3107', '3589', '574', '703', '704', '712', '731', '810', '857', '862', '909', '927', '980']
>>> int_list = [int(l) for l in list]
>>> int_list.sort()
>>> int_list
[574, 703, 704, 712, 731, 810, 857, 862, 909, 927, 980, 1008, 1033, 1080, 3107, 3589]

Or Simply you can do as below:

>>> print(sorted(list,key=int))
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
  • This does fix the issue of printing the actual list. However, for some reason it is still not being sorted. Both of these print it the same way as it is in the list. – Bigc1109 Feb 26 '20 at 04:20
  • I got it. It was list of characters. I have updated my answer – Venkataraman R Feb 26 '20 at 04:24
0

Try

sorted((int(i) for i in list),reverse=True)
Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
0

you are sorting the list of strings you can pass int as key to sort function, so it will call int() for every string in the list.

list = ['1008', '1033', '1080', '3107', '3589', '574', '703', '704', '712', '731', '810', '857', '862', '909', '927', '980']
list.sort(key=int)
print(list)
deadshot
  • 8,881
  • 4
  • 20
  • 39
-1

Try this:

list = ['1008', '1033', '1080', '3107', '3589', '574', '703', '704', '712', '731', '810', '857', '862', '909', '927', '980']

for i in range(0, len(list)): 
    list[i] = int(list[i])

list.sort(reverse = True) 
print(list)

'''
Alex
  • 36
  • 4
  • Note that the elements in the list are strings. They'd need to be converted to integers first! – Prateek Dewan Feb 26 '20 at 04:20
  • Whoops, let me fix that! – Alex Feb 26 '20 at 04:21
  • Thanks! This works perfectly. However, is there anyway to change the list of strings into integers without changing the actual list? As in keep the list as strings and update the list to change it to integers? – Bigc1109 Feb 26 '20 at 04:23
  • Yep! Create a new array on the side, let me add it in to my answer – Alex Feb 26 '20 at 04:24
  • Actually having trouble adding it to my answer, for unfortunately I must go. If you can complete it on your own, well done, but I'll finish the entire code tomorrow. And of course! – Alex Feb 26 '20 at 04:29
  • 1
    Naming a variable `list` is a bad idea. Why iterate over the indices of the list if all you do is access the element at the current index? – AMC Feb 26 '20 at 04:32
  • Oh, and `0` is the default value for the `start` parameter in `range()`. – AMC Feb 26 '20 at 04:43