So I have a list of version numbers, eg:
["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"]
and I want to order them from smallest to largest. I found that I can use the sorted() function, and that orders it well. Here's the function I came up with:
def order(l):
# Sorts numbers from smallest to largest
orderedList = sorted(l)
strList = ""
for i in range(len(orderedList)):
strList += orderedList[i]
# Don't put a comma for the last loop
if i < len(orderedList) - 1:
strList += ", "
print(strList)
So,
order(["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"])
currently outputs:
0.1, 1.1.1, 1.11, 1.2, 1.2.1, 2, 2.0, 2.0.0.
This may look right, but I need it to order differently.
My desired output is:
0.1, 1.1.1, 1.2, 1.2.1, 1.11, 2, 2.0, 2.0.0
In other words, I need to tell the program that 1.11 is like one.eleven, not a decimal one.oneone, and is therefore bigger than 1.2 or 1.2.1. Similar to Minecraft's versioning system; 1.18 is bigger than 1.8.9, and I need the function to order the program like that. But the sorted() function orders it so that 1.8.9 is bigger than 1.18
Is there an obvious solution that I've missed?
Is it more complicated than I anticipated?
Do I need to create my own ordering function as an alternative to sorted()?
Could I somehow use the key parameter in sorted to construct my own order? If so, how?
Thanks in advance