Results 1 to 4 of 4

Thread: [RESOLVED] Comparing version "strings"

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Resolved [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!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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 ...

  3. #3

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Comparing version "strings"

    are you kidding? no way.... hang on!

    Edit:

    Pure Beauty!! never new! Thanks!!! Saved me a ton of work! lol
    Last edited by Static; Aug 25th, 2010 at 11:17 AM.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Comparing version "strings"

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim versionStrings() As String = {"4.5.0.55", "4.2.2.128", "4.5.0.37", "5.0.0.713", "4.5.0.128"}
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'sort ascending
    7.         Array.Sort(versionStrings, New comparer)
    8.         MsgBox(versionStrings(versionStrings.GetUpperBound(0)))
    9.     End Sub
    10. End Class
    11.  
    12. Public Class comparer
    13.     Implements IComparer
    14.  
    15.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    16.         Dim xParts() As Integer = Array.ConvertAll(x.ToString.Split("."c), Function(s) CInt(s))
    17.         Dim yParts() As Integer = Array.ConvertAll(y.ToString.Split("."c), Function(s) CInt(s))
    18.  
    19.         If xParts(0) = yParts(0) Then
    20.             If xParts(1) = yParts(1) Then
    21.                 If xParts(2) = yParts(2) Then
    22.                     Return xParts(3).CompareTo(yParts(3))
    23.                 Else
    24.                     Return xParts(2).CompareTo(yParts(2))
    25.                 End If
    26.             Else
    27.                 Return xParts(1).CompareTo(yParts(1))
    28.             End If
    29.         Else
    30.             Return xParts(0).CompareTo(yParts(0))
    31.         End If
    32.  
    33.     End Function
    34. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width