ComponentOne Xamarin Edition
Sorting
Controls > CollectionView > Features > Sorting

CollectionView interface supports ascending and descending sorting for data controls. To enable sorting, add one or more SortDescription objects to the CollectionView's SortDescriptions property. To sort columns at runtime, You can simply tap the header of the list to sort data.

SortDescription objects are flexible, they allow you to add objects for individual columns, and set their sorting order to ascending or descending.

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

The following code example demonstrates how to sort a FlexGrid control in C# and XAML. This example uses the sample created in the FlexGrid's Quick Start section.

Import the following references in the class:
using Xamarin.Forms;
using C1.CollectionView;
using C1.Xamarin.Forms.Grid;

In Code

C#
Copy Code
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;
}

In XAML

XAML
Copy Code
<C1.Xamarin.Forms:Grid AllowSorting="True">