Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Sort generic collection

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
9 Apr 2008CPOL2 min read 44.6K   276   16  
This article describe how to sort generic collections
Imports System.Reflection
Imports System.Collections.Generic
Namespace CollectionSorter
    Public Class CollectionSorter(Of T)
        Implements IComparer(Of T)

#Region "Private Variables"
        Private _sortColumn As String
        Private _reverse As Boolean
#End Region

#Region "Constructor"
        Public Sub New(ByVal sortEx As String)
            'find if we want to sort asc or desc
            If Not String.IsNullOrEmpty(sortEx) Then
                _reverse = sortEx.ToLowerInvariant().EndsWith(" desc")

                If _reverse Then
                    _sortColumn = sortEx.Substring(0, sortEx.Length - 5)
                Else
                    If sortEx.ToLowerInvariant().EndsWith(" asc") Then
                        _sortColumn = sortEx.Substring(0, sortEx.Length - 4)
                    Else
                        _sortColumn = sortEx
                    End If
                End If

            End If
        End Sub
#End Region

#Region "Interface Implementation"

        Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare
            'get the properties of the objects
            Dim propsx() As PropertyInfo = x.GetType().GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public)
            Dim propsy() As PropertyInfo = x.GetType().GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public)
            Dim retval As Integer

            'find the column we want to sort based on
            For i As Integer = 0 To propsx.Length - 1
                If _sortColumn.ToLower() = propsx(i).Name.ToLower() Then
                    'find the type of column so we know how to sort
                    Select Case propsx(i).PropertyType.Name
                        Case "String"
                            retval = CStr(propsx(i).GetValue(x, Nothing)).CompareTo(CStr(propsy(i).GetValue(y, Nothing)))
                        Case "Integer"
                            retval = CInt(propsx(i).GetValue(x, Nothing)).CompareTo(CInt(propsy(i).GetValue(y, Nothing)))
                        Case "Int32"
                            retval = CInt(propsx(i).GetValue(x, Nothing)).CompareTo(CInt(propsy(i).GetValue(y, Nothing)))
                        Case "Int16"
                            retval = CInt(propsx(i).GetValue(x, Nothing)).CompareTo(CInt(propsy(i).GetValue(y, Nothing)))
                        Case "DateTime"
                            retval = CDate(propsx(i).GetValue(x, Nothing)).CompareTo(CDate(propsy(i).GetValue(y, Nothing)))
                    End Select

                End If
            Next

            If _reverse Then
                Return -1 * retval
            Else
                Return retval
            End If

        End Function
#End Region

#Region "Equal Function"

        Public Function Equals(ByVal x As T, ByVal y As T) As Boolean
            Dim propsx() As PropertyInfo = x.GetType().GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public)
            Dim propsy() As PropertyInfo = y.GetType().GetProperties(System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public)
            Dim retval As Boolean
            For i As Integer = 0 To propsx.Length - 1
                If _sortColumn.ToLower() = propsx(i).Name.ToLower() Then
                    retval = propsx(i).GetValue(x, Nothing).Equals(propsy(i).GetValue(y, Nothing))
                End If
            Next
            Return retval
        End Function
#End Region

    End Class
End Namespace


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Innovative Design
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions