Link to home
Start Free TrialLog in
Avatar of majnun
majnun

asked on

VB .NET:Implement a sort on a class's arraylist property, with different sorting schemes

I have a class b, that holds an arraylist of another class a. I want to be able to sort the arraylist in class b, based on various properties of each of the a class object it holds.

By way of psuedo-code example:

class a
   public property a1 as string...
   public property a2 as integer...
end class

class b
   private pAArraylist as arraylist

   public property AArraylist as arraylist
      get() return pAArraylist
      set(value) pAArraylist = value
   end property

   public function addA(AtoAdd as a)
     pAArraylist.add(AtoAdd)
   end function
end class

dim myB as new b

dim myA as new a
myA.a1 = "Hello World"
myA.a2 = 1
myB.addA(myA)

myA.a1 = "Goodbye"
myA.a2 = 2
myB.addA(myA)

myA.a1 = "Hello World"
myA.a2 = 3
myB.addA(myA)

What I want to do now is be able to say something like:
myB.sort(sortByA1)

or

myB.sort(sortByA2)

or

myB.sort(SortByA1 then sortByA2)

So when i loop through the myB.AArraylist it is sorted based on the various .sort functions explained above.

I hope this is clear.

Thanks for any help!!!

ASKER CERTIFIED SOLUTION
Avatar of ramesh12
ramesh12

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ramesh12
ramesh12

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.  This sounds a lot like what ADO.Net DataTable and DataView do via DataView.Sort().  Depending on your application, it might make sense to reuse this existing functionality.
Avatar of majnun

ASKER

Yeah it does seem like a lot of work...

Here's the particular nature of the problem I am trying to solve... I have a rather complicated database driven application that I have been using Access as a front end and back end for, and i am begining to feel the pain of VBA and so am switching slowly over to using VB .NET for everything.

The group that I am working for has a habit of changing their conceptual model of the things I am using the databases to keep track of, and previously I had everything tied to forms in Access that were generated by a myriad of SQL statements. A change to their conceptual model often requires a slight but fundamental restructure of the tables, sometimes adding new ones and new relationships... however, many of the functionalities of what I need to do with the data once I have generated an SQL query generally doesn't change.

I tried to create classes in VB that could abstractly represent the data, and build an "initialize" function for container class that connected to the databases, and populated the contained class's properties with data returned by SQL statements. And to create relationships between classes I created arraylists to hold sub classes.

For example, there is a series of tables representing albums and audio tracks. I created a class which held audio track objects (with properties such as Title, Artist, Duration, etc.) which in turn was shoved in an object based on an Album class (with a "trackArraylist" property, as well as properties such as album name, etc.) And the album class objects were in turn shoved into a "library class" which has an Album Arraylist, and an initialize() fuction which connects to the database and populates all these objects. This is a more simplified version of one of the many data relationships I need to keep track of.

My thought was that once I have these classes, I can go ahead and write code like that references these properties instead of the table views directly. This way if the structure of the database changes I can simply change how these properties are populated in the initialize function of the container class, and worse case if a property becomes obsolete I can just populate that property with an empty string, or 0, or some other "non-filled in" value.

I was hoping in this way to limit the amount of recoding when the database conceptual model and structure changes.

I was hoping that an Arraylist would be able to sort objects inside simply by refereing those object's properties which have standard types (like strings, integers, etc.) For instance if track myAlbum(x).title was "Bob's Song" it seems logical that if myAlbum(y).title is "Sally's Song" that the track myAlbum(x) would come before track myAlbum(y). And I was hoping to write a simple function i could use to sort based on album properties, or track properties like sort(myLibrary, album.title, ascending) or sort(myLibrary, track.duration, descending) and so on. Where arg1 is the library holding the albums (which are holding the tracks), arg2 is the class (album or track) and propery I want to sort by, and arg3 is the direction of the sort.

Maybe I should just stick with datasets and datareaders.

How do other people deal with abstracting their presentation of data from the actual datasets generated from underlying tables that might change (and that in my environment WILL change)?

For instance, do people use aliases and then if a field becomes obsolete just alias an empty field for all obselete fields?

Thanks!