

A couple of days ago I had to implement
You may be wondering why all this, why not compare just the version strings? Well, because it doesn't work :P For example, if we compare the strings '1.0.100.5' and '1.0.110.5' the result will be the first one is smaller, which is right:
But if we compare '1.0.100.5' against '1.0.11.5', the result is wrong, since it will say the first one is still smaller:
Obviously, that is not true, since the first version is greater or newer than the second one.
So I wanted to know how simple would be to do this in Python. I was picturing on my mind using regular expressions to extract all 4 version numbers (major, minor, release and build) and then comparing them as lists, which would return the good result.
So I implemented it and it was just as simple as I expected. Well, not exactly as simple as I expected since regular expressions return strings. I solved this using a combination of map and lambda functions:
Sometimes regular expressions are a little bit 'too much'. I mean, sometimes it's just better to use string functions which may work even faster:
As you can see, we still need the lambda function to convert the string parts to integers. Probably there is also a way to make this easier :)
Post new comment