【问题标题】:Editing code to order listbox items in descending numeric order编辑代码以按数字降序排列列表框项目
【发布时间】:2016-03-12 00:18:57
【问题描述】:

我正在尝试为该项目生成一份报告,其中显示哪些县(科克、卡洛等)最受欢迎。目前我已经能够找到客户县的每个迭代并将其显示在这样的列表框(lstReports)中......

Antrim= 0
Armagh= 1
Carlow= 2
Cavan=  1

以此类推

我想通过单击一个按钮来实现它,我可以按数字升序(btnNumericAc)和降序(btnNumericdec)对列表进行排序......

例如。升序

Antrim= 0
Armagh= 1
Cavan= 1
Carlow= 2

例如递减

Carlow= 2
Armagh= 1
Cavan= 1
Antrim= 0

我能够找到一段通过升序对我的项目进行排序的代码,我想知道我必须对代码进行哪些编辑才能通过降序排序。没有关于代码如何工作的解释,所以我不确定要进行哪些编辑。

代码:

类代码:

Public Class CountiesSorter 
Implements IComparer(Of String)

Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
    Dim xai As Integer = x.IndexOf(" ")
    Dim yai As Integer = y.IndexOf(" ")
    Select Case True
        Case xai = -1 AndAlso yai = -1
            Return x.CompareTo(y)
        Case xai = -1 AndAlso yai > -1
            Return -1
        Case xai > -1 AndAlso yai = -1
            Return +1
        Case xai > -1 AndAlso yai > -1
            Dim xs As String() = x.Split(" ")
            Dim ys As String() = y.Split(" ")
            Select Case xs(1).CompareTo(ys(1))
                Case 0 : Return xs(0).CompareTo(ys(0))
                Case -1 : Return -1
                Case +1 : Return +1
            End Select
    End Select
End Function
End Class

功能代码:

Public Sub SortAccendingListBox(ByRef lb As ListBox)
    Dim il As New List(Of String)
    For Each i As String In lb.Items
        il.Add(i)
    Next
    il.Sort(New CountiesSorter)
    lb.Items.Clear()
    lb.Items.AddRange(il.ToArray)
End Sub

按键代码:

Private Sub btnNumericAs_Click(sender As Object, e As EventArgs) Handles btnNumericAs.Click
SortAccendingListBox(lstReports)
End Sub

感谢您的帮助和时间。

【问题讨论】:

  • 从技术上讲,您需要告诉您的 Sorter 类进行降序排序,也许在构造函数中,然后只需翻转 Desc 的返回值。但是你最好使用一个简单的类来存储名称和值,这样你就可以按实际值而不是数字排序,因为“19”>“100”。
  • 你为什么要把事情搞得那么难?创建一个包含NamePopularityCounty 类。创建一个List<County> 并用县填充它。然后使用OrderBy(x=>x.Popularity)OrderByDescending(x=>x.Popularity) 对其进行排序。然后将排序结果设置为您的ListBoxDataSource 并将ListBoxDisplayName 设置为Name

标签: vb.net winforms visual-studio-2013 listbox


【解决方案1】:

创建一个包含NamePopularityCounty 类。
然后创建一个List<Conuty> 并用县填充它。
然后使用OrderBy(x=>x.Popularity)OrderByDescending(x=>x.Popularity) 对其进行排序。然后将排序结果设置为DataSourceListBox

代码

这是County类的代码:

Public Class County
    Public Property Name As String
    Public Property Popularity As Integer
    Public Overrides Function ToString() As String
        Return String.Format("{0}= {1}", Me.Name, Me.Popularity)
    End Function
End Class

这是Form的代码:

Public Class Form1
    Dim Counties As New List(Of County)
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Counties = New List(Of County) From
        {
            New County() With {.Name = "Antrim", .Popularity = 0},
            New County() With {.Name = "Armagh", .Popularity = 1},
            New County() With {.Name = "Carlow", .Popularity = 2},
            New County() With {.Name = "Cavan", .Popularity = 1}
        }
        Me.ListBox1.DataSource = Counties.OrderBy(Function(x) x.Popularity).ToList()
    End Sub
    Private Sub SortAscending_Click(sender As Object, e As EventArgs) Handles SortAscending.Click
        Me.ListBox1.DataSource = Counties.OrderBy(Function(x) x.Popularity).ToList()
    End Sub

    Private Sub SortDescending_Click(sender As Object, e As EventArgs) Handles SortDescending.Click
        Me.ListBox1.DataSource = Counties.OrderByDescending(Function(x) x.Popularity).ToList()
    End Sub
End Class

【讨论】:

  • 谢谢你做得很好,这是一种更清洁的方式。
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
相关资源
最近更新 更多