Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Generics Without Reflection

Status
Not open for further replies.

mdjoin

Programmer
Jun 22, 2007
9
US
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,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top