Sorting an array of objects!
-
I have an array of objects. Each object refers to an mp3 file as follows: mp3file.Title mp3file.Artst mp3file.Album mp3file.TrackNumber My question is, how can I sort the array by a specific item of the object, eg. Sorted by Artist, sorted by Album etc. Thnaks
-
I have an array of objects. Each object refers to an mp3 file as follows: mp3file.Title mp3file.Artst mp3file.Album mp3file.TrackNumber My question is, how can I sort the array by a specific item of the object, eg. Sorted by Artist, sorted by Album etc. Thnaks
-
Make a custom comparer for your class, and use that in a call to Array.Sort.
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.
Thanks for the quick reply. You say, 'Make a custom comparer for the class'. I'm fairly new to VB.net and don't quite understand what you mean. Please could you explain a little more. Thanks again
-
Thanks for the quick reply. You say, 'Make a custom comparer for the class'. I'm fairly new to VB.net and don't quite understand what you mean. Please could you explain a little more. Thanks again
Something like this. Say you want to sort by Artist name. Make a function: Function CompareArtist(ByVal Artist1 As String, ByVal Artist2 As String) As Boolean IF (Artist1>Artist2) Then Return True Else Return False End IF End Function Then when you do your sort, have something like this: If CompareArtist(mp3file(1).Artist,mp3file(2).Artist) Then //Do you sorting here depending on the result of the compareartist function. End If You could make a seperate comparison function for each member of the object, or just 1 or 2 depending on the type's of the object's members (if they are all strings for example). I hope that makes some sense. David
-
Something like this. Say you want to sort by Artist name. Make a function: Function CompareArtist(ByVal Artist1 As String, ByVal Artist2 As String) As Boolean IF (Artist1>Artist2) Then Return True Else Return False End IF End Function Then when you do your sort, have something like this: If CompareArtist(mp3file(1).Artist,mp3file(2).Artist) Then //Do you sorting here depending on the result of the compareartist function. End If You could make a seperate comparison function for each member of the object, or just 1 or 2 depending on the type's of the object's members (if they are all strings for example). I hope that makes some sense. David
Thanks very much, this is the sort of thing I was after. I guess you cant use array.sort on an array of objects. I though there was probably some syntax you could add into array.sort to specify which 'column' you wanted to sort by. Anyway, the code you have given me David looks fine. Thanks
-
Thanks for the quick reply. You say, 'Make a custom comparer for the class'. I'm fairly new to VB.net and don't quite understand what you mean. Please could you explain a little more. Thanks again
Create a class that implements the interface IComparer. The class needs to have a method named Compare that does the comparison of the properties you want to sort. I usually don't program VB, but something like this:
Class MyObjectComparer Inherits IComparer
Public Function Compare(A as Object, B as Object)
Return String.Compare(DirectCast(A, MyClass).SomeProperty, DirectCast(B, MyClass).SomeProperty)
End FunctionEnd Class
This you can use to sort your array of MyClass objects:
Dim comparer as new MyObjectComparer() Array.Sort(TheArray, comparer)
--- It's amazing to see how much work some people will go through just to avoid a little bit of work.