How do you sort input in Python?

Question:

beginning coder here. I was trying to sort words and numbers, and it mostly work. However, when I provided input to sort, it doesn’t seem to work.

I tried inserting the input as a variable and it broke the numbers down into single digits, which I did not want to do. I tried converting the input into a float and integers and it did not work. I tried

input1 = input() #I input in 23, 12, 45
sorted(input1)

And I got

[' ', ' ', ',', ',', '1', '2', '2', '3', '4', '5']

I was expecting for it to sort by whole numbers without being broken down.

Asked By: BeginningCoder04

||

Answers:

If your input is a String, you can convert it to int and add to a list.
Then you can sort the list:

list =  []
x = input("write:")
x = x.replace(" ","")
y = x.split(",")
for i in y:
    if i.isnumeric():
        i = int(i)
        list.append(i)
list.sort()
print(list)
Answered By: Miro
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.