Compound Collection Sort

Therefore, WPF does not support the standard sorting or filtering behavior for CompositeCollections, so it would be best practice to solve this problem.

There are two or more collections of objects of different types. You want to combine them into one sortable and filtered collection (if necessary, manually sort or filter).

One approach that I examined is to create a new collection of objects with several kernel properties, including the ones I would like to sort, and an instance of an object of each type.

class MyCompositeObject { enum ObjectType; DateTime CreatedDate; string SomeAttribute; myObjectType1 Obj1; myObjectType2 Obj2; { class MyCompositeObjects : List<MyCompositeObject> { } 

And then scroll through my two object collections to put together a new composite collection. Obviously, this is a bit crude method, but it will work. I would get all the default sorting and filtering behavior in my new collection of compound objects, and I could place a data template on it to display list items correctly, depending on what type is actually stored in that compound element.

What suggestions exist for this in a more elegant way?

+6
collections c # data-binding wpf
source share
3 answers

Update: I found a much more elegant solution:

 class MyCompositeObject { DateTime CreatedDate; string SomeAttribute; Object Obj1; { class MyCompositeObjects : List<MyCompositeObject> { } 

I found that due to reflection, the specific type stored in Obj1 is allowed at runtime, and the DataTemplate type is applied as expected!

+1
source share

The brute force method you are talking about is actually the perfect solution. Keep in mind that all objects are in RAM, there is no I / O bottleneck, so you can pretty much sort and filter millions of objects in less than a second on any modern computer.

The most elegant way to work with collections is the System.Linq namespace in .NET 3.5

Thank you - I also considered LINQ for objects, but my concern is the loss of flexibility for typed data templates, which I need to display objects in my list.

If you cannot currently predict how people sort and filter your collection of objects, then you should take a look at the System.Linq.Expressions namespace to create your lambda expressions on demand at runtime (first you let the user create expressions, then compile, run, and in the end you use the reflection namespace to list the results). It’s more difficult to wrap your head around you, but an invaluable function, perhaps (for me definitively) an even more innovative function than LINQ itself.

+1
source share

I'm not very familiar with WPF yet, but I see this as a matter of sorting and filtering List<T> collections.

(if necessary, manually sort or filter)

Would you reconsider implementing your own sort or filter functions? In my experience this is easy to use. The examples below use an anonymous delegate, but you can easily define your own method or class to implement complex sorting or filtering. Such a class may even have properties for setting and changing sorting and filtering dynamically.

Use List<T>.Sort(Comparison<T> comparison) with a special comparison function:

 // Sort according to the value of SomeAttribute List<MyCompositeObject> myList = ...; myList.Sort(delegate(MyCompositeObject a, MyCompositeObject b) { // return -1 if a < b // return 0 if a == b // return 1 if a > b return a.SomeAttribute.CompareTo(b.SomeAttribute); }; 

A similar approach for getting subnets of items from a list.

Use List<T>.FindAll(Predicate<T> match) with a custom filter function:

 // Select all objects where myObjectType1 and myObjectType2 are not null myList.FindAll(delegate(MyCompositeObject a) { // return true to include 'a' in the sub-collection return (a.myObjectType1 != null) && (a.myObjectType2 != null); } 
+1
source share

All Articles