Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Sorting an array of objects!

Sorting an array of objects!

Scheduled Pinned Locked Moved Visual Basic
questionalgorithmsdata-structures
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    portreathbeach
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • P portreathbeach

      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

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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.

      P 1 Reply Last reply
      0
      • G Guffa

        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.

        P Offline
        P Offline
        portreathbeach
        wrote on last edited by
        #3

        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

        D G 2 Replies Last reply
        0
        • P portreathbeach

          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

          D Offline
          D Offline
          dhbaer
          wrote on last edited by
          #4

          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

          P 1 Reply Last reply
          0
          • D dhbaer

            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

            P Offline
            P Offline
            portreathbeach
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • P portreathbeach

              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

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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 Function

              End 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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups