0

I'm a beginner programmer and I'm trying to make an exercise.

I want to sort an integer list, but every time I run my code, the list is not sorted. I've tried it in a couple of different ways with sorted() or .sort(), but nothing seems to help.

def main():

    _list1_ = []
    _list2_ = []

    print("Enter random numbers and enter Q to quit: ")
    userInput1 = input("")
    while userInput1.upper() != "Q":
        _list1_.append(int(userInput1))
        userInput1 = input("")

    print("Enter random numbers and enter Q to quit:")
    userInput2 = input("")
    while userInput2.upper() != "Q":
        _list2_.append(int(userInput2))
        userInput2 = input("")

    sorted(_list1_)
    sorted(_list2_)

    print(_list1_)

main()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bvdabjorn
  • 69
  • 1
  • 7
  • (This was the ***top*** search engine hit ([DDG](https://en.wikipedia.org/wiki/DuckDuckGo)) for *"how to sort list of integers in Python"* (used in the 2023 blog post *"[Ask like a human: Implementing semantic search on Stack Overflow](https://stackoverflow.blog/2023/07/31/)"*)... It is an example of the typical low-scored duplicates in search engine results.) – Peter Mortensen Jul 31 '23 at 19:57
  • For that, something like *[How to sort a list of strings numerically](https://stackoverflow.com/questions/3426108/)* (2010), *[Sort a list of numerical strings in ascending order](https://stackoverflow.com/questions/9758959)* (2012), and *[How to sort a Python list of strings of numbers](https://stackoverflow.com/questions/17474211)* (2013). Though there *must* be a canonical from 2008 or 2009. – Peter Mortensen Jul 31 '23 at 21:28

3 Answers3

7

sorted() doesn't sort the list in place. It returns the sorted list, so you will need to change the 2 sorted() calls to something like this:

_list1_ = sorted(_list1_)
_list2_ = sorted(_list2_)

It's always a good idea to read the documentation to get an understanding for how the functions work. Here is the docs for sorted https://docs.python.org/2/library/functions.html#sorted

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Sendoa
  • 86
  • 4
2

sorted returns the sorted list whereas sort performs the sort in place.

So you could either do:

_list1_ = sorted(_list_)

or:

_list1_.sort()

If you were to use sort (my preferred method) your code would look like this:

def main():

    _list1_ = []
    _list2_ = []

    print("Enter random numbers and enter Q to quit: ")
    userInput1 = input("")
    while userInput1.upper() != "Q":
        _list1_.append(int(userInput1))
        userInput1 = input("")

    print("Enter random numbers and enter Q to quit:")
    userInput2 = input("")
    while userInput2.upper() != "Q":
        _list2_.append(int(userInput2))
        userInput2 = input("")

    _list1_.sort()
    _list2_.sort()

    print(_list1_)

main()
Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
0
sorted(_list1_) 

returns a list after sorting list1, It does not sort the list1. so write

print(sorted(_list1_)) 

or assign the sorted list1 to list1, like

_list1_ = sorted(_list1_)
Eular
  • 1,707
  • 4
  • 26
  • 50