[RESOLVED] Comparing version "strings"
I cant seem to think this morning...
I am trying to compare version strings. These are scraped off a website...
examples:
4.5.0.55
4.2.2.128
4.5.0.37
5.0.0.713
how do i make sure I am grabbing the highest version?
right now I have it checking the length, if less than 9 .. add a zero. then remove the .'s and convert to an integer. then just check. I know thats not the best and most accurate. Thats why Im asking LOL
help my tired brain....
THANKS!
edit:
yep just spotted a problem
if the one is 4.5.0.55 and i compare to 4.5.0.128 it thinks the 55 is newer because it added the 0!
sigh... help!
Re: Comparing version "strings"
What you do is use the Version datatype built into .net :).
Code:
Dim FirstVersion As New Version("4.5.0.100")
Dim SecondVersion As New Version("4.5.0.99")
If FirstVersion.CompareTo(SecondVersion) > 0 Then ...
Re: Comparing version "strings"
are you kidding? no way.... hang on!
Edit:
Pure Beauty!! never new! Thanks!!! Saved me a ton of work! lol
Re: Comparing version "strings"
try this:
vb Code:
Public Class Form1
Dim versionStrings() As String = {"4.5.0.55", "4.2.2.128", "4.5.0.37", "5.0.0.713", "4.5.0.128"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'sort ascending
Array.Sort(versionStrings, New comparer)
MsgBox(versionStrings(versionStrings.GetUpperBound(0)))
End Sub
End Class
Public Class comparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim xParts() As Integer = Array.ConvertAll(x.ToString.Split("."c), Function(s) CInt(s))
Dim yParts() As Integer = Array.ConvertAll(y.ToString.Split("."c), Function(s) CInt(s))
If xParts(0) = yParts(0) Then
If xParts(1) = yParts(1) Then
If xParts(2) = yParts(2) Then
Return xParts(3).CompareTo(yParts(3))
Else
Return xParts(2).CompareTo(yParts(2))
End If
Else
Return xParts(1).CompareTo(yParts(1))
End If
Else
Return xParts(0).CompareTo(yParts(0))
End If
End Function
End Class