Override
the Comparer
property[
^] with a custom
IComparer
instance that sorts as you want it to.
Something like this should work:
Class EnumDescriptionComparer : Implements IComparer
Private ReadOnly _enumType As Type
Public Sub New(ByVal enumType As Type)
_enumType = enumType
End Sub
Private Function GetDescription(ByVal value As Object) As String
Dim fi As FieldInfo = _enumType.GetField([Enum].GetName(_enumType, value))
Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)
Return If(dna Is Nothing, value.ToString(), dna.Description)
End Function
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim xDescription As String = GetDescription(x)
Dim yDescription As String = GetDescription(y)
Return StringComparer.OrdinalIgnoreCase.Compare(xDescription, yDescription)
End Function
End Class
Class DescriptionConverter : Inherits EnumConverter
Private ReadOnly _enumType As Type
Private ReadOnly _comparer As IComparer
Public Sub New(ByVal type As Type)
MyBase.New(type)
_enumType = type
_comparer = new EnumDescriptionComparer(type)
End Sub
Protected Override ReadOnly Property Comparer As IComparer
Get
Return _comparer
End Get
End Property
...
End Class