none
Sort types by ancestry? RRS feed

  • Question

  • I have an array of many class types.   Some of the types in the array may inherit from other types in the array.  I need to be able to group / sort the related types so that any base types occur first followed by their descendant types.

    Example:

    Unsorted: Cat, Dog, Tiger, Poodle, Car

    Sorted:  Cat, Tiger, Dog, Poodle, Car

    Is there a good way to do this in .NET?

    Sunday, August 26, 2012 8:29 PM

Answers

  • Hi Scott,

    Thank you for posting on this forum.

    I agree with the Icomparer interface, and you can implements it like this:

    Public Class MySortorClass
        Implements IComparer
    
        Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim bType As Type = x.GetType
            Dim aType As Type = y.GetType
            If bType = aType Then
                Return 0
            End If
            While Not bType IsNot Type.GetType("Object")
                If bType.BaseType = aType Then
                    Return 1
                End If
            End While
            While Not aType IsNot Type.GetType("Object")
                If bType.BaseType = aType Then
                    Return -1
                End If
            End While
            ' if there is no inheritence relationship, consider them as euquals to each other
            Return 0
        End Function
    End Class

    and use it like this:

            Dim a As New AClass
            Dim b As New BClass
            Dim str As String = "sadfasdfas"
    
            Dim ol As New List(Of Object)
            ol.AddRange({a, b, str})
            Dim customerComparor As New MySortorClass()
            ol.Sort(customerComparor)

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by scott_m Wednesday, August 29, 2012 9:19 PM
    Wednesday, August 29, 2012 1:31 PM
    Moderator

All replies

  • Your should write a custom sort algorithm to implement this kind ( any kind ) of sorting. For this you need to either go for implementing IComparable (http://msdn.microsoft.com/en-us/library/system.icomparable.aspx) or IComparer (http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx)

    Am sure this will solve your issue.

    Monday, August 27, 2012 5:14 AM
  • Hi Scott,

    Thank you for posting on this forum.

    I agree with the Icomparer interface, and you can implements it like this:

    Public Class MySortorClass
        Implements IComparer
    
        Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim bType As Type = x.GetType
            Dim aType As Type = y.GetType
            If bType = aType Then
                Return 0
            End If
            While Not bType IsNot Type.GetType("Object")
                If bType.BaseType = aType Then
                    Return 1
                End If
            End While
            While Not aType IsNot Type.GetType("Object")
                If bType.BaseType = aType Then
                    Return -1
                End If
            End While
            ' if there is no inheritence relationship, consider them as euquals to each other
            Return 0
        End Function
    End Class

    and use it like this:

            Dim a As New AClass
            Dim b As New BClass
            Dim str As String = "sadfasdfas"
    
            Dim ol As New List(Of Object)
            ol.AddRange({a, b, str})
            Dim customerComparor As New MySortorClass()
            ol.Sort(customerComparor)

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by scott_m Wednesday, August 29, 2012 9:19 PM
    Wednesday, August 29, 2012 1:31 PM
    Moderator