Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a program that sorts the user's input in increasing and decreasing order... I have tried the sorted() function but it would only sort 1 number.

What I have tried:

Python
list=input("Enter your list:")
print("Unsorted:", list.split())

a=list
print("Sorted Ascending Order:")
print sorted(a)

print("Sorted Descending Order:")
print sorted(a, reverse=True)



In my code, I tried to sort 35,5 and 4 but the result would be like this:
['3','4','5','5']
Posted
Updated 13-Apr-21 1:03am
v3

1 solution

The following program (Python 3)
Python
s = input()
lnum = list(map(int, s.split()))

print("Unsorted:", lnum)

print("Sorted Ascending Order:")
print (sorted(lnum))

print("Sorted Descending Order:")
print(sorted(lnum, reverse=True))

executes this way
35 5 4                                                                                                                                                     
Unsorted: [35, 5, 4]                                                                                                                                       
Sorted Ascending Order:                                                                                                                                    
[4, 5, 35]                                                                                                                                                 
Sorted Descending Order:                                                                                                                                   
[35, 5, 4] 
 
Share this answer
 
Comments
lil_mint 13-Apr-21 10:34am    
Thank you very much, this helps a lot..
CPallini 13-Apr-21 10:50am    
You are welcome.
Maciej Los 13-Apr-21 15:52pm    
5ed!
CPallini 14-Apr-21 1:45am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900