ComponentOne Basic Library for UWP
Basic Library Overview / CollectionView for UWP / Getting Started with C1CollectionView / Sorting C1CollectionView
In This Topic
    Sorting C1CollectionView
    In This Topic

    You can sort items in your collection using C1CollectionView just as you would using a CollectionView implementation in any other platform. When you sort a C1CollectionView, the underlying data set is not affected.

    For example you can sort by using the SortDescriptions property passing in a SortDescription object:

    Visual Basic
    Copy Code
    Dim list = New System.Collections.ObjectModel.ObservableCollection(Of Customer)()
    ' create a C1CollectionView from the list
    Dim _view As New C1.Xaml.C1CollectionView(list)
    ' sort customers by country
    _view.SortDescriptions.Add(New C1.Xaml.SortDescription("Country", C1.Xaml.ListSortDirection.Ascending))
    

    C#
    Copy Code
    var list = new System.Collections.ObjectModel.ObservableCollection<Customer>();
    // create a C1CollectionView from the list
    C1.Xaml.C1CollectionView _view = new C1.Xaml.C1CollectionView(list);
    // sort customers by country
    _view.SortDescriptions.Add(new C1.Xaml.SortDescription("Country", C1.Xaml.ListSortDirection.Ascending));
    

    Where "Country" is the name of the property which you want to sort on. You can sort ascending (A-Z) or descending (Z-A) depending on the ListSortDirection parameter.

     

    Sorting on more than one property or column

    You can sort on more than one property by simply adding additional SortDescriptions. If you are sorting by multiple properties you should use the DeferRefresh method to defer the automatic refresh after each sort so that you only apply each sort once.

    Visual Basic
    Copy Code
    ' sort multiple properties using DeferRefresh so you only refresh once
    Using _view.DeferRefresh()
          _view.SortDescriptions.Clear()
          _view.SortDescriptions.Add(New C1.Xaml.SortDescription("Country", C1.Xaml.ListSortDirection.Ascending))
          _view.SortDescriptions.Add(New C1.Xaml.SortDescription("Name", C1.Xaml.ListSortDirection.Ascending))
    End Using
    

    C#
    Copy Code
    // sort multiple properties using DeferRefresh so you only refresh once
    using (_view.DeferRefresh())
    {
        _view.SortDescriptions.Clear();
        _view.SortDescriptions.Add(new C1.Xaml.SortDescription("Country", C1.Xaml.ListSortDirection.Ascending));
        _view.SortDescriptions.Add(new C1.Xaml.SortDescription("Name", C1.Xaml.ListSortDirection.Ascending));
    }
    
    Note: If the collection implements INotifyCollectionChanged any changes to the data will be applied to the sort even after it’s been set.

    For more advanced sorting that can improve performance see the C1CollectionView.CustomSort property.

    See Also