【问题标题】:Order list based on first two numbers in the string - .net基于字符串中前两个数字的订单列表 - .net
【发布时间】:2016-05-25 20:16:02
【问题描述】:

所以我有 10 个列表框和一个 openfiledialog。我使用“添加”按钮打开一个 .txt 文件,并根据其包含的字符串将该文件中的每一行排序到不同的列表框。

以下是 .txt 文件中的三行:

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 采用 .crc 字符串,Listbox 2 采用 .hash 字符串,等等...

我可以根据扩展名将文件过滤到适当的列表框,但我现在想做的是根据降序版本号对每个列表框进行排序(扩展名之后但大小之前的数字:ex 1.0) .

我尝试了以下代码,但显然失败了。

'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)

任何帮助将不胜感激。

【问题讨论】:

  • item 字符串是什么样的?
  • 我看不到您的字符串中的分隔符“- (”在拆分方法中使用的位置
  • @DonBoitnott 我作为示例提供的三行是每个项目。
  • @Steve Ahh 是的,我已经解决了

标签: .net vb.net winforms listbox


【解决方案1】:
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

编辑

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)

【讨论】:

  • 谢谢。我将如何将其实施到我的项目中?抱歉,我在这方面有点菜鸟。
  • 1.我会从文件中获取行。 2. 我会填充一个 List(Of string) 并对其进行排序。 3.我会通过listbox.Items.AddRange(list.ToArray)将列表添加到列表框中
  • 是的,我明白代码在做什么,我只是不明白如何将这个模块插入到我的项目中。它只是在我的 form1.vb 代码中吗?
  • Class RowComparer 复制到您的文件中,但在Class Fomr1 下,因此在文件中您有两个类。
  • 你也可以在你的项目中添加一个模块——命名为 HelperModule——并在这个模块中添加类。这个模块可以作为所有项目常用事物的“存储库”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 2014-04-11
  • 1970-01-01
  • 2021-01-16
相关资源
最近更新 更多