ComponentOne Xamarin.Mac
Controls / CollectionView / Features / Sorting
In This Topic
    Sorting
    In This Topic

    Sorting refers to arranging data in an ordered or meaningful sequence to analyze it effectively. When sorting is applied to a grid, the data in the columns is sorted in ascending or descending order. The ICollectionView interface supports ascending and descending sorting for data controls, such as grids. It implements SortAsync method to allow you to call the sorting operation in the collection view without having to cast to the specific interface. The SortAsync method sorts the collection view according to the specified sort path and direction. To sort columns at runtime, you can simply tap the column header to sort that particular column of the TableView control.

    The image below shows how TableView appears when sorting is performed.

    The following code examples demonstrate how to sort the collection view according to the specified sort path and direction.

    C#
    Copy Code
    using System;
    
    using AppKit;
    using Foundation;
    using C1.CollectionView;
    using C1.Mac.CollectionView;
    using System.Threading.Tasks;
    using System.Linq;
    
    namespace C1CollectionViewTest
    {
        public partial class SortingViewController : NSViewController
        {
            public SortingViewController(IntPtr handle) : base(handle)
            {
                
            }
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                Load();
                // Do any additional setup after loading the view.
            }
    
            private async Task Load()
            {
                var dataCollection = Customer.GetCustomerList(100);
                C1CollectionView<Customer> cv = new C1CollectionView<Customer>(dataCollection);
                var sort = cv.SortDescriptions.FirstOrDefault(sd => sd.SortPath == "Name");
                var direction = sort != null ? sort.Direction : SortDirection.Descending;
                await cv.SortAsync(x => x.Name, direction == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
                var source = new C1TableViewSource(TableView);
                source.ItemsSource = cv;
                TableView.Source = source;
            }
    
            public override NSObject RepresentedObject
            {
                get
                {
                    return base.RepresentedObject;
                }
                set
                {
                    base.RepresentedObject = value;
                    // Update the view, if already loaded.
                }
            }
        }
    }