JustReflektor
JustReflektor

Reputation: 95

Editing code to order listbox items in descending numeric order

I'm trying to generate a Report for the project which shows which counties (Cork,Carlow, etc.) are most popular. Currently I have been able to find each iteration of a customers county and display it in a list box(lstReports) like this...

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

and so on so forth

I wanted to make it so on the click of a button I could sort the list in numeric acceding order(btnNumericAc) and descending order(btnNumericdec)...

eg. Accending

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

e.g. Decending

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

I was able to find a piece of code that orders my project by ascending, I was wondering what edits I would have to make to the code to order by decending. There was no explanation about how the code worked, so I am unsure what edits to make.

The code:

Class Code:

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

Function Code:

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

Button Code:

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

Thank you for your help and time.

Upvotes: 1

Views: 76

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

Create a County class containing Name and Popularity.
Then Create a List<Conuty> and fill it with counties.
Then sort it using OrderBy(x=>x.Popularity) or OrderByDescending(x=>x.Popularity). Then set the sort result as DataSource of your ListBox.

Code

Here is the code for County class:

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

Here is the code for 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

Upvotes: 1

Related Questions