编辑代码以按数字降序排列列表框项



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

感谢您的帮助和时间。

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

法典

以下是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

最新更新