majnun
asked on
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.Collect ionBase
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.Messa geBox.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!
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.Collect
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.Messa
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
This is in C# but is what you want
http://www.dotnet247.com/247reference/msgs/25/125273.aspx
http://www.dotnet247.com/247reference/msgs/25/125273.aspx
This is in VB.NET
http://aspalliance.com/461
http://aspalliance.com/461
see this link for getting properties and then getting and setting there values
Hope it helps