# Binding SortOrder in PropertyGroupDescriptor in CollectionView

## Environment

| Version | Product                                 | Author                                                                        |
| ------- | --------------------------------------- | ----------------------------------------------------------------------------- |
| 13.0.0  | Telerik UI for .NET MAUI CollectionView | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |

## Description

I attempted to bind the `SortOrder` property in the [PropertyGroupDescriptor](https://www.telerik.com/maui-ui/documentation/controls/collectionview/grouping/property-group-descriptor) in a grouped CollectionView, but encountered the error:

`No property, BindableProperty, or event found for "SortOrder", or mismatching type between value and property.`

This issue arises because the `SortOrder` property is not bindable. I need a solution to dynamically manage the sort order.

This knowledge base article also answers the following questions:

* How can I implement dynamic sorting in CollectionView?
* How do I use attached properties for `PropertyGroupDescriptor` in .NET MAUI?
* How can I change the sort order programmatically in CollectionView?

## Solution

Use one of the following approaches to manage the `SortOrder` dynamically:

* [Approach 1: Managing GroupDescriptors Collection](https://www.telerik.com/maui-ui/documentation/knowledge-base/binding-sortorder-propertygroupdescriptor-radcollectionview#approach-1-managing-groupdescriptors-collection)
* [Approach 2: Using Attached Property](https://www.telerik.com/maui-ui/documentation/knowledge-base/binding-sortorder-propertygroupdescriptor-radcollectionview#approach-2-using-attached-property)

### Approach 1: Managing GroupDescriptors Collection

Clear and re-add the `GroupDescriptors` collection with the desired `SortOrder`:

```csharp
this.collectionview.GroupDescriptors.Clear();
this.collectionview.GroupDescriptors.Add(new PropertyGroupDescriptor
{
    PropertyName = nameof(ReceiptModel.TransactionDateMonth),
    SortOrder = SortOrder.Descending
});
```

### Approach 2: Using Attached Property

Define an attached property on the CollectionView level to bind and update the `SortOrder`. Follow these steps:

**1.** Create a static class to define the attached property:

```csharp
public static class TelerikUtils
{
    public static readonly BindableProperty MySortOrderProperty =
        BindableProperty.CreateAttached("MySortOrder", typeof(SortOrder), typeof(TelerikUtils), SortOrder.Ascending, propertyChanged: OnMySortOrderChanged);

    public static SortOrder GetMySortOrder(BindableObject bindable)
    {
        return (SortOrder)bindable.GetValue(MySortOrderProperty);
    }

    public static void SetMySortOrder(BindableObject bindable, SortOrder value)
    {
        bindable.SetValue(MySortOrderProperty, value);
    }

    private static void OnMySortOrderChanged(BindableObject bindable, object oldValue, object newValue)
    {
        RadCollectionView cv = (RadCollectionView)bindable;
        var descriptor = cv.GroupDescriptors.FirstOrDefault();
        SortOrder mySortOrder = (SortOrder)newValue;
        descriptor.SortOrder = mySortOrder;
    }
}
```

**2.**  Bind the `SortOrder` property to the `ViewModel` using the attached property in XAML:

```xaml
<telerik:RadCollectionView local:TelerikUtils.MySortOrder="{Binding SortingOrder}" ...>
    <telerik:RadCollectionView.GroupDescriptors>
        <telerik:PropertyGroupDescriptor PropertyName="TransactionDateMonth" />
    </telerik:RadCollectionView.GroupDescriptors>
</telerik:RadCollectionView>
```

**3.** Create a `ViewModel` with a property for `SortingOrder`:

```csharp
public class MainPageViewModel : NotifyPropertyChangedBase
{
    private SortOrder sortingOrder = SortOrder.Ascending;

    public SortOrder SortingOrder
    {
        get => sortingOrder;
        set
        {
            if (sortingOrder != value)
            {
                sortingOrder = value;
                OnPropertyChanged();
            }
        }
    }
}
```

**4.** For example, on a button or another UI element modify the `SortingOrder`:

```csharp
private void Button_Clicked(object sender, EventArgs e)
{
    ViewModel.SortingOrder = ViewModel.SortingOrder == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}
```

## See Also

* [Grouping with RadCollectionView](https://www.telerik.com/maui-ui/documentation/controls/collectionview/grouping/overview)
* [CollectionView Overview](https://www.telerik.com/maui-ui/documentation/controls/collectionview/overview)
* [Bindable Properties in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/attached-properties?view=net-maui-10.0)
