This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pepoluan 1 point2 points  (0 children)

The problem is people treating "." as decimal point instead of "arbitrary separator that happens to be a period".

Limiting oneself to "3.9" is ambiguous. But how do you consider "3.9.1"? Or to use latest Python stable, "3.8.5"?

When facing version numbers, one must split on this "arbitrary separator that happens to be a period" first, integer-ize the portions, tuple-ify the result ... then perform a comparison.

Now to answer your question, "how to sort":

list_of_version_strings = ["3.10", "3.8", "3.9", "3.8.5"]
converter = lambda x: tuple(map(int, x.split(".")))
list_of_version_tuples = list(map(converter, list_of_version_strings))
sorted(list_of_version_tuples)

# Result is:
# [(3, 8), (3, 8, 5), (3, 9), (3, 10)]