Dom Sinclair Dom Sinclair - 1 year ago 54 Question

What is the best way to compare two product / assembly versions

Let us say that a product has an assembly version number of 1.5.4537.56784. There are plenty of examples of how to retrieve this on SO and in most cases (if you simply want to display the information to the end user) then a default string representation is fine.

I now find myself wanting to compare this assembly number with another (this time most definitely stored as a string as it's in a text file online (but in the same format)) to determine whether the version number stored online is the same as the version number retrieved from the executing assembly or it is in fact greater. The term greater is important, not just different but actively greater.

A simple string compare ought to determine if they are the same but I don't think it would determine greater than. So what type ought I consider changing these stings to in order to make the greater than comparison. Or have I missed something obvious with string comparisons.

Answer Source

Split your values by . into an Integer array or list, whichever you prefer. Then use a simple If x > y comparison for each position in the collection.

Dim myAssemblyVersion = "1.5.4537.56784"
Dim theirAssemblyVersion = "1.5.4827.1"

Dim myIntegers = myAssemblyVersion.Split(".")
Dim theirIntegers = theirAssemblyVersion.Split(".")

Dim isTheirsgreater As Boolean = False
For i = 0 To myIntegers.Count - 1
    If theirIntegers(i) > myIntegers(i) Then
        isTheirsgreater = True
    End If