user3407086 user3407086 - 6 months ago 19
Vb.net 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: 25.61.221.29
Generic Hasher.hash, 1.9, 63 MB: 25.61.221.29
Basic Port Scan.scan, 1.0, 23 MB: 25.61.221.29


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
ListBox1.Items.Clear()
ListBox1.Items.AddRange(items)


Any help would be appreciated.

Answer
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))
            Next
            ' 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))
            Next
            ' 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: 25.61.221.29",
                                        "Generic Hasher.hash, 1.20, 63 MB: 25.61.221.29",
                                        "Basic Port Scan.scan, 1.10, 23 MB: 25.61.221.29",
                                        "Basyyyyyyyyy.scan, 2.10, 23 MB: 25.61.221.29",
                                        "BasixxxxScan.scan, 1.12, 23 MB: 25.61.221.29"})

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

        Console.ReadLine()
    End Sub

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

End Module

EDIT

Dim lines As String() = IO.File.ReadAllLines("C:\Trial\list.txt")
Dim list as new List(Of String)(lines)
list.Sort(New RowComparer)
ListBox1.Items.AddRange(list.ToArray)