2016-03-12 41 views
1

我試圖生成,它展示了其縣(黃柏,卡洛等)是最流行的項目報告列表框中的項目。目前,我已經能夠找到一個客戶全縣每個迭代,並在列表框中顯示出來(lstReports)這樣的...編輯代碼訂購降序數字順序

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

等等等等

我想讓它這樣在按鈕的點擊,我可以在排序數字即將加入順序(btnNumericAc)和降序(btnNumericdec)列表...

如。正在加載

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

例如Decending

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

我能找到一段代碼,通過上升的訂單我的項目,我想知道我的編輯將不得不作出的代碼通過decending訂購。沒有關於代碼如何工作的解釋,所以我不確定做什麼編輯。

代碼:

類代碼:

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 

謝謝您的幫助和時間。

+0

Techinally,你需要告訴你的分揀機類進行排序降序,也許在構造函數中,然後只是將Desc的返回值翻轉過來。但是使用一個簡單的類來存儲名稱和值將會更好,因此您可以按實際值排序而不是數字,因爲「19」>「100」。 – Plutonix

+0

爲什麼你要讓事情變得艱難?創建一個包含「Name」和「Popularity」的縣級類。創建一個'清單'並填寫縣。然後使用'的OrderBy(X => x.Popularity)'或'OrderByDescending(X => x.Popularity)'排序。然後將排序結果設置爲ListBox的DataSource並將ListBox的DisplayName設置爲Name。 –

回答

1

創建包含NamePopularity一個County類。
然後創建一個List<Conuty>並與縣填充它。
然後使用OrderBy(x=>x.Popularity)OrderByDescending(x=>x.Popularity)排序。然後將排序結果設置爲ListBoxDataSource

代碼

下面是County類的代碼:爲Form

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 
+1

華麗的答案,簡單和簡單! – Codexer

+1

謝謝你的工作很棒,這是一個更乾淨的方式。 – JustReflektor