Python Forum
Sorting numbers from smallest to biggest - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Sorting numbers from smallest to biggest (/thread-25886.html)



Sorting numbers from smallest to biggest - Dokugan - Apr-14-2020

Hi, i im new in Python, and i have troubles with understanding why my code cant sort numbers from smallest to biggest :
a = input ('a Size: ')
b = input ('b Size: ')
c = input ('c Size: ')
if a>b and a>c:
    C = a
elif c<b>a:
        C=b
else:
            C=c
if a<b and a<c:
    A=a
elif b<a and b<c:
        A=b
else:
            A=c
if b<a<c or b>a>c:
    B=a
elif a<b<c or a>b>c:
        B=b
else: B=c
print (A,B,C)
        

I feel like numbers are just randomly bounded with A,B,C letters.


RE: Sorting numbers from smallest to biggest - bowlofred - Apr-14-2020

input() hands you back a string. Strings will sort (and compare) alphabetically. So '10' < '5'. If you want numeric comparisons, you'll need to convert them to ints or similar.

int('5') < int('10')



RE: Sorting numbers from smallest to biggest - Larz60+ - Apr-14-2020

suggest using natsort:
pypi entry: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
github: https://github.com/SethMMorton/natsort
python install pip install natsort
you can also do it using regex: https://stackoverflow.com/a/5967539