I'm working on a project that contains 3 different lists. 2 lists of 2 different controls, and 1 list of a custom class. I found that I needed each of these lists sorted. The problem with sorting a list of complex types is there's no default comparer for them. That means using a custom routine to sort by a specific property. Probably the most common property that will have a unique value to each member is the 'Name' property. Instead of having 3 different sort routines to sort by the 'Name' property, I wanted to come up with a generic routine that would sort a list of any object that has a 'Name' property. I tried investigating other solutions, but I found them to be quite complicated, probably because I haven't learned Lambda yet. Thus the accompanying very simple code. The problem I faced was getting the name string when the generic Object
type doesn't have a 'Name' property. I found that for the custom class I had to set up 'Name' as a property, I used the Auto-Implented Property({ get; set; }
). I haven't tried this with other collections but it should work with any collection that is made up of a type with a 'Name' property and has a Sort()
method, that uses IComparer<T>
. I haven't tried it yet, but I don't see any reason why this wouldn't work for VB as well.
Ketsuekiame
860
Master Poster
Featured Poster
tinstaafl
1,176
Posting Maven
Ketsuekiame
860
Master Poster
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.