In order to be able to sort you will have to implement an instance of the icomparer.
The first change is to the "BindToEnum" function:
Private
Function BindToEnum(ByVal enumType As Type) As ArrayList()
Dim names As String(), values As Array
Dim x As Integer = 0
names = System.Enum.GetNames(enumType)
values = System.Enum.GetValues(enumType)
Dim arrayOfEnum(names.Length - 1) As HW.ArraySort
While x < names.Length
arrayOfEnum(x) =
New HW.ArraySort(CType(values.GetValue(x), Integer), names(x))
x = x + 1
End While
Array.Sort(arrayOfEnum)
Return arrayOfEnum
End Function
You will now be returning an ArrayList. You will also need to add the following code in a module file:
Option
Explicit On
Option
Strict On
Imports
System.Collections
Public
Class ArraySort : Inherits System.Collections.ArrayList : Implements IComparable
' Nested classes to do secondary sorts
Private Class sortByValueAscendingHelper : Implements IComparer
Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim c1 As ArraySort = CType(a, ArraySort)
Dim c2 As ArraySort = CType(b, ArraySort)
If (c1.Value > c2.Value) Then
Return 1
End If
If (c1.Value < c2.Value) Then
Return -1
Else
Return 0
End If
End Function
End Class
Private Class sortByValueDescendingHelper : Implements IComparer
Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim c1 As ArraySort = CType(a, ArraySort)
Dim c2 As ArraySort = CType(b, ArraySort)
If (c1.Value < c2.Value) Then
Return 1
End If
If (c1.Value > c2.Value) Then
Return -1
Else
Return 0
End If
End Function
End Class
Private Class sortKeyDescendingHelper : Implements IComparer
Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
Dim c1 As ArraySort = CType(a, ArraySort)
Dim c2 As ArraySort = CType(b, ArraySort)
Return String.Compare(c2.mKey, c1.mKey)
End Function
End Class
' End nested classes.
Private mValue As Integer
Private mKey As String
Public Sub New(ByVal Value As Integer, ByVal Key As String)
mKey = Key
mValue = Value
End Sub
Public Property Value() As Integer
Get
Return mValue
End Get
Set(ByVal Value As Integer)
mValue = Value
End Set
End Property
Public Property Key() As String
Get
Return mKey
End Get
Set(ByVal Value As String)
mKey = Value
End Set
End Property
Function CompareTo(ByVal obj As Object) As Integer Implements IComparable.CompareTo
Dim c As ArraySort = CType(obj, ArraySort)
Return String.Compare(Me.mKey, c.mKey)
End Function
Public Shared Function sortByValueAscending() As IComparer
Return CType(New sortByValueAscendingHelper, IComparer)
End Function
Public Shared Function sortByValueDescending() As IComparer
Return CType(New sortByValueDescendingHelper, IComparer)
End Function
Public Shared Function sortByKeyDescending() As IComparer
Return CType(New sortKeyDescendingHelper, IComparer)
End Function
End
Class
Now you are able to sort by KEY (display data) or Value either ascending or descending.
Hopefully this solves your issue..