Python Forum
Sorting numbers from smallest to biggest
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting numbers from smallest to biggest
#1
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.
Reply
#2
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')
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  smallest Cosine distance in Graph vino689 3 2,275 Jan-12-2020, 08:06 AM
Last Post: rmspacedashrf
  Tracking 2 biggest objects in OpenCV Nup 0 1,665 Nov-12-2019, 11:08 AM
Last Post: Nup
  I want to check for the smallest value entered by the user Shilpa 2 1,775 Aug-13-2019, 12:06 PM
Last Post: ThomasL
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,668 May-09-2019, 12:19 PM
Last Post: Pleiades
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 2,997 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  Sorting lists prioritizing letters before numbers Ratherfastmofo 5 3,175 Nov-21-2018, 06:44 PM
Last Post: woooee
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 2,743 Oct-28-2018, 10:48 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020