我正试图为该项目编写一份报告,显示哪些县(科克、卡洛等)。是最受欢迎的。目前,我已经能够找到客户县的每一个迭代,并将其显示在列表框(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谢谢你的帮助和时间。
发布于 2016-03-12 01:57:52
创建一个包含County和Popularity的类。
然后创建一个List<Conuty>并用县填充它。
然后使用OrderBy(x=>x.Popularity)或OrderByDescending(x=>x.Popularity)对其进行排序。然后将排序结果设置为DataSource of您的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 Classhttps://stackoverflow.com/questions/35952152
复制相似问题