user3407086 user3407086 - 2 years ago 75 Question

Order list based on first two numbers in the string - .net

So I have 10 listboxes and one openfiledialog. I use the "Add" button to open a .txt file and sort every line in that file to the different listboxes depending on the string it contains.

Here are three lines from the .txt file:

Decent Cracker.crc, 2.0, 75 MB:
Generic Hasher.hash, 1.9, 63 MB:
Basic Port Scan.scan, 1.0, 23 MB:

Listbox 1 takes .crc strings, Listbox 2 takes .hash strings, and etc...

I have no problem filtering the files to their proper listboxes based on their extension, but what I would like to do now is sort each listbox based on descending version number (the number after the extension but before the size: ex 1.0).

I have tried the following code but it obviously failed.

'sort by first number
Dim items = (From item In ListBox1.Items
Let parts = item.ToString.Split(New String() {" - ("}, StringSplitOptions.None)
Order By CInt(parts(0))
Select item).ToArray

Any help would be appreciated.

Answer Source
Module Module1
    Public Class RowComparer
        Implements IComparer(Of String)

        Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
            Dim xCodeParts As String() = x.Split(","c)(1).Trim.Split("."c) ' something like 1.1
            Dim yCodeParts As String() = y.Split(","c)(1).Trim.Split("."c) ' something like 1.20

            Dim xCodeText As String = ""
            Dim yCodeText As String = ""

            For i As Integer = 0 To xCodeParts.Length - 1
                xCodeParts(i) = xCodeParts(i).PadLeft(10, "0"c)
                xCodeText &= String.Format("|{0}|", xCodeParts(i))
            ' xCodeText is something like |0000000001||0000000001|

            For i As Integer = 0 To yCodeParts.Length - 1
                yCodeParts(i) = yCodeParts(i).PadLeft(10, "0"c)
                yCodeText &= String.Format("|{0}|", yCodeParts(i))
            ' yCodeText is something like |0000000001||0000000020|

            Return -xCodeText.CompareTo(yCodeText) ' - for desc order    
        End Function
    End Class

    Sub Main()
        Dim list As New List(Of String)({
                                        "Decent Cracker.crc, 1.1, 75 MB:",
                                        "Generic Hasher.hash, 1.20, 63 MB:",
                                        "Basic Port Scan.scan, 1.10, 23 MB:",
                                        "Basyyyyyyyyy.scan, 2.10, 23 MB:",
                                        "BasixxxxScan.scan, 1.12, 23 MB:"})

        list.Sort(New RowComparer)
        list.ForEach(Sub(value) Console.WriteLine(value))

    End Sub

'Basyyyyyyyyy.scan, 2.10, 23 MB:
'Generic Hasher.hash, 1.20, 63 MB:
'BasixxxxScan.scan, 1.12, 23 MB:
'Basic Port Scan.scan, 1.10, 23 MB:
'Decent Cracker.crc, 1.1, 75 MB:

End Module


Dim lines As String() = IO.File.ReadAllLines("C:\Trial\list.txt")
Dim list as new List(Of String)(lines)
list.Sort(New RowComparer)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download