Custom sorting logic for groups using CollectionViewSource
Asked Answered
K

1

6

I have a list of "stuff" which needs to be filtered then displayed in a grouped and sorted manner. The data needed to calculate the grouping and sorting order is not available as simple properties - there needs to be some work done in code to calculate the order and groups.

CollectionViewSource lets me define custom filter and sort logic - so far so good. It also lets me bind GroupDescriptions to a value converter so that I can generate the group names.

The last thing I want to do is control the order that the generated groups appear and this is causing me pain!

Everything I see about CollectionViewSource.SortDescriptions says that it will sort groups by a property name, but I don't have a property available to sort by. SortDescriptions can't be bound to a value converter like GroupDescriptions can and I'm out of other ideas.

So - how do you implement custom sorting logic of CollectionViewSource groups?

Kaon answered 11/5, 2010 at 8:2 Comment(0)
P
5

This post on Bea Stollnitz' blog, and the GitHub repo, demonstrates how you can do that. You will have to sort by the criteria of your grouping first. Even if this is not a concrete property, it should be possible to sort your items using the same logic that you use to group them, isn't it?! Certainly, this is not possible using a SortDescription instance, but you could instead use the ListCollectionView.CustomSort property and specify an appropriate IComparer implementation.

Puttier answered 11/5, 2010 at 9:4 Comment(4)
That didn't seem to work for me when I tried it, but I'll go back and take another look, since I've tried a bunch of other stuff since then and it's quite possible I missed something...Kaon
Well, it works for me. Maybe you could post some code, then we could try to find your problem.Puttier
OK, I had a "couldn't see the wood for the trees" moment. Thanks - after clearing out all the cruft that did what I was looking for.Kaon
That webpage is not longer available.Barroom

© 2022 - 2024 — McMap. All rights reserved.