Resolved ListView PropertyGroupDescription Custom Sort

archie456

Member
Joined
Apr 19, 2020
Messages
11
Programming Experience
1-3
Hi,

I have a ListView bound to a view model, I set up groupings in the list view using PropertyGroupDescriptions:

C#:
            BrowserItemCollectionView = CollectionViewSource.GetDefaultView(myCollection);

            //Set up filter
            BrowserItemCollectionView.Filter = FilterBrowserItems;

            //Set up grouping
            PropertyGroupDescription L1PGD = new PropertyGroupDescription(nameof(BrowserItem.L1group));
            PropertyGroupDescription L2PGD = new PropertyGroupDescription(nameof(BrowserItem.L2group));
            PropertyGroupDescription L3PGD = new PropertyGroupDescription(nameof(BrowserItem.L3group));
            BrowserItemCollectionView.GroupDescriptions.Add(L1PGD);
            BrowserItemCollectionView.GroupDescriptions.Add(L2PGD);
            BrowserItemCollectionView.GroupDescriptions.Add(L3PGD);

To then sort the groups using this:

C#:
            //Setup Sorting
            BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L1group), ListSortDirection.Ascending));
            BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L2group), ListSortDirection.Ascending));
            BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L3group), ListSortDirection.Ascending));
C#:

This all works nicely, but I'd like to sort one of the groups by my own custom method, but I can't see how I can specify this:

C#:
            BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L1group), MYSORTING));

But I can't see any way to sort a SortDesciption using a custom sort.

I'd appreciate some pointers on the best way to do this.
Thanks.
 
Cast view to ListCollectionView and set CustomSort to a class instance that implements IComparer.
 
Ok, but how would I apply the sorting to a particular SortDescription - i.e.

BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L2group), MYSORTING));

Thanks.
 
SortDescription is for sorting specific property asc/desc using default comparison. With CustomSort you control the sorting yourself entirely.
Setting this property clears a previously set SortDescriptions value.
 
@JohnH - Its a custom sort to a specific property that I need to achieve, as I'm writing an addon to some software and trying to match their sorting, this only occurs on a specific level of the tree... the rest of the control alphabetic sorting is fine. With CustomSort can I apply this to only one property? (I can't see how...)

@jmcilhinney - Thanks for that. I've set up a string length comparer as per your example as a test.
 
this only occurs on a specific level of the tree
That is not true, all items are sorted according to either SortDescriptions or CustomSort. You can add as many sorting levels as you want, and can perform any kind of comparison between the objects.

In part 1 there's an example, here's another, a type User is sorted by Age/LastName/FirstName properties:
C#:
class UserSort : IComparer<User>, IComparer
{
    public int Compare(User x, User y)
    {
        var result = x.Age.CompareTo(y.Age);
        if (result == 0)
            result = x.LastName.CompareTo(y.LastName);
        if (result == 0)
            result = x.FirstName.CompareTo(y.FirstName);
        return result;
    }

    int IComparer.Compare(object x, object y)
    {
        return Compare((User)x, (User)y);
    }
}
While only IComparer is used, I perfer to set up sorting with IComparer<T> and just route the non-generic to that.
This example is the same as adding a SortDescription for each of those three properties ascending, but the Compare method is a lot more flexible in how you can compare.
 
Hi,

Thanks for your response, I'm actually using the IComparer routine from that blog, the one that compared string length (as a test routine)

The point I'm struggling with is how to apply the custom sorting to a particular PropertyGroupDescrption.

The ListView i'm using has 6 PropertyGroupDescriptions I set up the PropertyGroupDescriptions with:

C#:
PropertyGroupDescription L1PGD = new PropertyGroupDescription(nameof(BrowserItem.L1group));

5 of the 6 Groups I need ascending sort, which I set with:

C#:
BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L1group), ListSortDirection.Ascending));

But one of the group I need special sorting with the IComparer class that I've created - How do I set the sorting to this class, as this doesn't work:

C#:
BrowserItemCollectionView.SortDescriptions.Add(new SortDescription(nameof(BrowserItem.L1group), MySpecialSorting));

Am I missing something?
Cheers.
 
You can't mix SortDescriptions and CustomSort. You set up all levels of sorting within the Compare method.
In the example I posted there is 3 sorting levels applied. 3 properties are compared, first by Age, then by LastName, then by FirstName. Just like if you had added 3 SortDescriptions for those 3 properties.
 

Latest posts

Back
Top Bottom