Vb.net 编辑以数字降序排列列表框项目的代码
我正试图为该项目生成一份报告,显示哪些县(科克、卡洛等)最受欢迎。目前,我已经能够找到每个迭代的客户县,并显示在一个列表框(lstReports)像这样Vb.net 编辑以数字降序排列列表框项目的代码,vb.net,winforms,visual-studio-2013,listbox,Vb.net,Winforms,Visual Studio 2013,Listbox,我正试图为该项目生成一份报告,显示哪些县(科克、卡洛等)最受欢迎。目前,我已经能够找到每个迭代的客户县,并显示在一个列表框(lstReports)像这样 Antrim= 0 Armagh= 1 Carlow= 2 Cavan= 1 等等 我想让它在点击一个按钮,我可以排序的数字顺序(btnNumericAc)和降序(btnNumericdec)列表 例如:阿肯丁 Antrim= 0 Armagh= 1 Cavan= 1 Carlow= 2 e、 g.下降 Carlow= 2 Armagh=
Antrim= 0
Armagh= 1
Carlow= 2
Cavan= 1
等等
我想让它在点击一个按钮,我可以排序的数字顺序(btnNumericAc)和降序(btnNumericdec)列表
例如:阿肯丁
Antrim= 0
Armagh= 1
Cavan= 1
Carlow= 2
e、 g.下降
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
感谢您的帮助和时间。创建一个
县
类,包含名称
和受欢迎程度
然后创建一个
列表
并用县填充。然后使用
OrderBy(x=>x.Popularity)
或OrderByDescending(x=>x.Popularity)
对其进行排序。然后将排序结果设置为列表框的数据源
代码
以下是县
类的代码:
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
以下是表单的代码
:
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
从技术上讲,您需要告诉您的Sorter类按降序排序,可能是在构造函数中,然后翻转Desc的返回值。但是您最好使用一个简单的类来存储名称和值,这样您就可以按实际值而不是数字进行排序,因为“19”>“100”。为什么要这么难呢?创建一个包含Name
和Popularity
的country
类。创建一个列表
,并用县填充它。然后使用OrderBy(x=>x.Popularity)
或OrderByDescending(x=>x.Popularity)
对其进行排序。然后将排序结果设置为ListBox
的DataSource
,并将ListBox
的DisplayName
设置为Name
。谢谢你的工作非常好,这是一种更干净的方法。