Python Forum

Full Version: Sorting numbers from smallest to biggest
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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')
suggest using natsort:
pypi entry: https://blog.miguelgrinberg.com/post/the...ello-world
github: https://github.com/SethMMorton/natsort
python install pip install natsort
you can also do it using regex: https://stackoverflow.com/a/5967539