Sort a collection of classes by class properties

I have a class with a bunch of properties (mostly strings, one date time)

I have a class which inherits the collectionBase that holds the above mentioned classes.

I want to be able to create a sort function that sorts the collection by the properties of the classes it contains.

Here is the code i have so far:

Public Class MediaObject
    Private myFilename As String
    Private myTitle As String
    Private myArtist As String
    Private myAlbum As String
    Private myDuration As DateTime

    Public Property filename() As String
        Get
            Return myFilename
        End Get
        Set(ByVal Value As String)
            myFilename = Value
        End Set
    End Property
    Public Property title() As String
        Get
            Return myTitle
        End Get
        Set(ByVal Value As String)
            myTitle = Value
        End Set
    End Property
    Public Property artist() As String
        Get
            Return myArtist
        End Get
        Set(ByVal Value As String)
            myArtist = Value
        End Set
    End Property
    Public Property album() As String
        Get
            Return myAlbum
        End Get
        Set(ByVal Value As String)
            myAlbum = Value
        End Set
    End Property
    Public Property duration() As DateTime
        Get
            Return myDuration
        End Get
        Set(ByVal Value As DateTime)
            myDuration = Value
        End Set
    End Property
End Class

Public Class MediaObjectCollection
    Inherits System.Collections.CollectionBase

    Public ReadOnly Property Item(ByVal index As Integer) As MediaObject
        Get
            ' The appropriate item is retrieved from the List object and
            ' explicitly cast to the MediaObject type, then returned to the
            ' caller.
            Return CType(List.Item(index), MediaObject)
        End Get
    End Property

    Public Sub Add(ByVal MediaObjectToAdd As MediaObject)
        list.Add(MediaObjectToAdd)
    End Sub

    Public Sub Remove(ByVal index As Integer)
        ' Check to see if there is a MediaObject at the supplied index.
        If index > Count - 1 Or index < 0 Then
            ' If no MediaObject exists, a messagebox is shown and the operation is
            ' cancelled.
            System.Windows.Forms.MessageBox.Show("Index not valid!")
        Else
            ' Invokes the RemoveAt method of the List object.
            List.RemoveAt(index)
        End If
    End Sub
End Class


So I want to be able to something like this:

dim mc as new mediaobjectcollection

mc.add(mediaObject1)
mc.add(mediaObject2)
mc.add(mediaObject3)
mc.add(mediaObject4)

mc.Sort(duration)
mc.Sort(title)
...and so on...

And if it is possible, can I write the Sort function so that it dynamically figures out what properties are in the mediaObject so if I add a new property or delete a property from the mediaObject class I don't have to rewrite the mediaObjectCollection's sort function?

Thank you for any help!
majnunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

armoghanCommented:
You need to use Reflection for this.
see this link for getting properties and then getting and setting there values

Hope it helps
0
armoghanCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
majnunAuthor Commented:
armoghan gave me useful info, but I'm still not sure how to sort them. The biggest delima is writing the function for the mediaobjectcollection for sorting it's collection based on the properties of the items in its collection.

I don't feel my question has been fully answered. I'd give armoghan assisted answer points, but since no one has really answered my question I can't close the question with an accepted answer.

I've moved on with a different approach in the particular project, so if you want to close this question, my suggestion is to find a way of giving armoghan some points (it doesn't matter to me how much, 100-500) but close the question without making his answer "accepted" since it doesn't offer the complete answer. It would also be nice to see if someone can figure out the sorting problem, so if you want to keep the question open for a while that's fine too (though it's been open and no one has taken a crack at it).

Either way, i'm fine with whatever.

Thanks!
0
armoghanCommented:
0
armoghanCommented:
This is in VB.NET
http://aspalliance.com/461
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.