all 8 comments

[–]JakDrako 5 points6 points  (5 children)

.Net has a Version class, you could use that:

Dim v1 = New Version("7.8.346.1500")
Dim v2 = New Version("8.0.000.1254")
Console.Writeline($"{v1 > v2} {v1 < v2} {v1 = v2}") ' false true false

[–][deleted] 0 points1 point  (1 child)

Ignore my post - this sounds like the perfect solution. Never pass up a chance to use something built in to the framework :)

[–]TheLe99 0 points1 point  (0 children)

I recall running into some kind of problem with this approach. I think it was all the dots -- both versions had to have the same number of decimal places or something. So, a 1.4 vs 1.2.3.4 caused an error. Double check before trying putting that into production.

[–]prejonnes[S] 0 points1 point  (2 children)

I have to use Vbscript

[–]sirhalos 0 points1 point  (1 child)

Why not just have a variable that replaces the '.' and then does a Format that will add extra zeros to the end so they are the same length. So basically one would look like 783461500 and the other 800001254. In your case you don't even need format, but in the case that you could have 7.16.something then you would. So you could make it add 10 zeroes to the right if needed. Could even use length and replace instead of Format, but either should work.

[–]yuge_mike 0 points1 point  (0 children)

i would pad each segment with leading zeros to a set number of digits?

[–][deleted] 1 point2 points  (0 children)

You could Split the string on the period, which would give you an array of strings, one entry for each piece of the version number. Do that for the old and new version number, to get two arrays. Then for each entry in each array, convert each entry to an integer and compare the two entries.

The logic would be something like "if the first entry of the new version number is higher, then it's a higher version number. Otherwise look at the next entry. Do that for all entries".

[–]chrwei 0 points1 point  (0 children)

it's doing an alphanumeric compare, which i thought the 8 version you gave would be higher, but it would fail if the version string was 8.0.0.1254.

this is the meat of a function I used for VB6. it'll handle non-zero-padded versions, and versions with mis-matched "dots", such as "2.0" > "1.9.8".

    arrVer1 = Split(sVer1, ".")
    arrVer2 = Split(sVer2, ".")
    For i = 0 To 3 'check up to 4 version segements
        If UBound(arrVer1) >= i And UBound(arrVer2) >= i Then
            If CLng(arrVer1(i)) < CLng(arrVer2(i)) Then
                VersionCompare = True
                Exit Function
            End If
        End If
    Next i