# Sorting

## Content



FlexGrid allows you to sort grid's data using the following methods:

*   **Sorting at Runtime**: The [AllowSorting](/componentone/api/xamarin/online-forms/dotnet-api/C1.Xamarin.Forms.Grid/C1.Xamarin.Forms.Grid.FlexGrid.AllowSorting.html) property of FlexGrid allows users to sort the grid. On setting the **AllowSorting** property to **true**, a user can simply tap a column's header to sort the grid by that column at runtime.
*   **Sorting through Code**: Using CollectionView interface, you can enable sorting through code in C# or XAML. To enable **sorting**, add one or more objects to the DataCollection.SortDescriptions property.

#### Sorting at runtime

The image below shows how the FlexGrid appears after sorting is applied to the grid by tapping header of the column, **Country**.

#### In Code

![](https://cdn.mescius.io/document-site-files/images/fb6b46a2-eac0-487c-84c3-5e1b4c7b1348/images/flexgridsortingrun.png)

```csharp
grid.AllowSorting = true;
```

#### In XAML

```xml
<c1:FlexGrid AllowSorting="True">
```

#### Sorting through Code

The image below shows how the FlexGrid appears after sorting is applied to the column **Name**.

![](https://cdn.mescius.io/document-site-files/images/fb6b46a2-eac0-487c-84c3-5e1b4c7b1348/images/flexgridsorting.png)

The following code example demonstrates how to sort a FlexGrid control in **C# and XAML**. This example uses the data source, **Customer.cs** created in the [Quick start](/componentone/docs/xamarin/online-forms/controls/OverviewFlexGrid/flexGridQuickStart) section.


> type=note
> Import the following references in the class:
> 
> ```
> using Xamarin.Forms;
> using C1.CollectionView;
> using C1.Xamarin.Forms.Grid;
> ```

#### In Code

```csharp
public static FlexGrid GetGrid()
       {        
         var dataCollection = Customer.GetCustomerList(10);
         C1CollectionView<Customer> cv = new C1CollectionView<Customer>(dataCollection);
         var sort = cv.SortDescriptions.FirstOrDefault(sd => sd.SortPath == "Name");
         var direction = sort != null ? sort.Direction : SortDirection.Descending;
         cv.SortAsync(x => x.Name, direction == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
         FlexGrid _grid = new FlexGrid();
         _grid.ItemsSource = cv;
         _grid.VerticalOptions = LayoutOptions.FillAndExpand;
         return _grid;
         cv.SortChanged += cv_SortChanged;
        }
     static void cv_SortChanged(object sender, EventArgs e)
      {
        throw new NotImplementedException();
      }
```

### Sorting a column by a different field

By default, sorting is applied on the bound field. However, you can sort a column by a different field. All you need to do is set the **SortMemberPath** property to the column by which you want the grid to be sorted. For example, the following column is bound to "FullName" but sorts by "LastName".

```csharp
column.Binding = “FullName”;
column.SortMemberPath = “LastName”;
```