2
votes

I have a collectionviewsource of items. Each item is an object, which has a property SentTime and another property ParentId. I want to group the items by their ParentId and to sort them in the following way:

  • the items in a group should be sorted by SentTime ascending
  • the groups should be sorted by SentTime descending

Here is a short example:

  • Item 1 Sent 1/10, parent Id 1
  • Item 2 Sent 2/10, parent id 1
  • Item 3 Sent 3/10, parent id 3
  • Item 4 Sent 4/10, parent id 3
  • Item 5 Sent 5/10, parent id 1

Then the grouped list should look like this:

Item 3

  • Item 4

Item 1

  • Item 2
  • Item 5

I have my collectionviewsource in the ViewModel.

this.MyCvs.SortDescriptions.Clear();
this.MyCvs.SortDescriptions.Add(new SortDescription("SendTime", ListSortDirection.Ascending));
this.MyCvs.GroupDescriptions.Add(new PropertyGroupDescription("ParentId"));

I know, that to order groups in CVS i just need to add another SortDescription by the property, by which the items are grouped (in my case ParentId) but it is not giving the result I want to obtain. Thank all for help in advance.

1

1 Answers

-1
votes

When you have grouping and sorting in a grid you have to set the sorting descriptions a bit differently than you did.

this.MyCvs.SortDescriptions.Clear();

this.MyCvs.GroupDescriptions.Add(new PropertyGroupDescription("ParentId"));

this.FilteredMessages.SortDescriptions.Add(new SortDescription("ParentId", ListSortDirection.Ascending));
this.FilteredMessages.SortDescriptions.Add(new SortDescription("SendTime", ListSortDirection.Ascending));

That is how you may achive that sorting shall only be applied per groups.