Sorting an ICollection on more than one field

Posted on 2005-02-24
Medium Priority
Last Modified: 2008-03-28
I have a custom Collection called Subscriptions. It is basically an ArrayList of objects of type Subscription.

SubScription is a class with 3 fields - Group, Title and URL.

When I load the subscriptions, they will be represented in a TreeView - the Title of each Subscription is displayed.

Subscriptions that have the same Group type should show in a Folder of the same name in the TreeView.

So when I load the treeview, I want to display the Folders (Groups) in alphabetical order, and the Subscriptions (Titles) in Alphabetical order.

So - it should be sorted by Group, then by Title.

Here is the code for the Subscriptions Class, and the accompanying IComparers for the sorts:
Public Class Subscriptions
    Implements ICollection
    Private al As ArrayList = New ArrayList

    Default Public Overloads ReadOnly _
    Property Item(ByVal index As Integer) As SubScription
            Return CType(al(index), Subscription)
        End Get
    End Property

    Public Sub CopyTo(ByVal a As Array, ByVal index As Integer) Implements ICollection.CopyTo
        al.CopyTo(a, index)
    End Sub

    Public ReadOnly Property Count() As Integer Implements ICollection.Count
            Count = al.Count
        End Get
    End Property

    Public ReadOnly Property SyncRoot() As Object Implements ICollection.SyncRoot
            Return Me
        End Get
    End Property

    Public ReadOnly Property IsSynchronized() As Boolean Implements ICollection.IsSynchronized
            Return False
        End Get
    End Property

    Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return al.GetEnumerator()
    End Function

    Public Function Add(ByVal newSubscription As SubScription) As Integer
        Return al.Count
    End Function

    Public Function Sort()
        Dim GroupSorter As IComparer = New GroupComparer
    End Function
End Class

Public Class GroupComparer
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim s1 As String = LCase(CType(x, Subscription).Group)
        Dim s2 As String = LCase(CType(y, Subscription).Group)
        Return s1.CompareTo(s2)
    End Function
End Class

Public Class TitleComparer
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim s1 As String = LCase(CType(x, Subscription).Title)
        Dim s2 As String = LCase(CType(y, Subscription).Title)
        Return s1.CompareTo(s2)
    End Function
End Class
Ok, if you look at the Sort() function, it is sorting by Group. If I was simply to sort again by Title, it would sort everything, and lose the group sort.

SO HERE IS THE QUESTION - How do I write this, so that when I call Sort() it sorts everything by Group and Title?

Question by:toddhd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

Lacutah earned 2000 total points
ID: 13394237
You want your Subscriptions Class to implement the IComparable Interface.  To get it to sort by First Group then Title, use the following for the CompareTo Function:

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        If Me.Group = CType(obj, Subscription).Group Then
            'Groups are the same, return comparison on Title
            Return Me.Title.CompareTo(CType(obj, Subscription).Title)
            Return Me.Group.CompareTo(CType(obj, Subscription).Group)
        End If
    End Function

To sort, use "Array.Sort()"  (no arguments) since it will by default use the IComparable interface.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question