Currently I've been using this little strip of code to sort my custom collections:
Public Function Compare(ByVal x As Branches, ByVal y As Branches) As Integer Implements System.Collections.Generic.IComparer(Of Branches).Compare
Dim xValue As Object = x.GetType().GetProperty(Me.SortColumn).GetValue(x, Nothing)
Dim yValue As Object = y.GetType().GetProperty(Me.SortColumn).GetValue(y, Nothing)
If SortingOrder = SortOrder.Decending Then
Return CType(yValue.ToString(), IComparable).CompareTo(xValue.ToString())
Else
Return CType(xValue.ToString(), IComparable).CompareTo(yValue.ToString())
End If
End Function
I'm trying to get away from the slight reflection used here, because I want to avoid the performance hit.
Does anyone have an idea/link to information about dynamic sorting of generics? I'm looking for something that is as fast as hard-coded strongly typed sorting, but that is flexible enough to be reused.
Thanks,
Public Function Compare(ByVal x As Branches, ByVal y As Branches) As Integer Implements System.Collections.Generic.IComparer(Of Branches).Compare
Dim xValue As Object = x.GetType().GetProperty(Me.SortColumn).GetValue(x, Nothing)
Dim yValue As Object = y.GetType().GetProperty(Me.SortColumn).GetValue(y, Nothing)
If SortingOrder = SortOrder.Decending Then
Return CType(yValue.ToString(), IComparable).CompareTo(xValue.ToString())
Else
Return CType(xValue.ToString(), IComparable).CompareTo(yValue.ToString())
End If
End Function
I'm trying to get away from the slight reflection used here, because I want to avoid the performance hit.
Does anyone have an idea/link to information about dynamic sorting of generics? I'm looking for something that is as fast as hard-coded strongly typed sorting, but that is flexible enough to be reused.
Thanks,