Sortable Observable Collection : ObservableCollection « Collections Data Structure « C# / C Sharp






Sortable Observable Collection

      
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace AdvancementVoyage.Magic.Utility
{
    /// <summary>
    /// An observable collection that can be sorted.
    /// </summary>
    /// <typeparam name="T">The type of item contained in the observable collection.</typeparam>
    [Serializable]
    internal sealed class SortableObservableCollection<T> : ObservableCollection<T>
    {
        /// <summary>
        /// Initializes a new instance of the SortableObservableCollection
        /// class.
        /// </summary>
        public SortableObservableCollection()
            : base() { }

        /// <summary>
        /// Initializes a new instance of the SortableObservableCollection class 
        /// that contains elements copied from the specified collection.
        /// </summary>
        /// <param name="collection">
        /// The collection from which the elements are copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The collection parameter cannot be a null reference.
        /// </exception>
        public SortableObservableCollection(IEnumerable<T> collection)
            : base(collection) { }

        /// <summary>
        /// Initializes a new instance of the SortableObservableCollection
        /// class that contains elements copied from the specified list.
        /// </summary>
        /// <param name="list">
        /// The list from which the elements are copied.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The list parameter cannot be a null reference.
        /// </exception>
        public SortableObservableCollection(List<T> list)
            : base(list) { }

        /// <summary>
        /// Sorts the items of the collection in ascending order according to a key.
        /// </summary>
        /// <typeparam name="TKey">
        /// The type of the key returned by <paramref name="keySelector"/>.
        /// </typeparam>
        /// <param name="keySelector">
        /// A function to extract a key from an item.
        /// </param>
        public void Sort<TKey>(Func<T, TKey> keySelector)
        {
            this.InternalSort(Items.OrderBy(keySelector));
        }

        /// <summary>
        /// Sorts the items of the collection in ascending order according to a key.
        /// </summary>
        /// <typeparam name="TKey">
        /// The type of the key returned by <paramref name="keySelector"/>.
        /// </typeparam>
        /// <param name="keySelector">
        /// A function to extract a key from an item.
        /// </param>
        /// <param name="comparer">
        /// An <see cref="IComparer{T}"/> to compare keys.
        /// </param>
        public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
        {
            this.InternalSort(Items.OrderBy(keySelector, comparer));
        }

        /// <summary>
        /// Moves the items of the collection so that their orders are the same as those of the items provided.
        /// </summary>
        /// <param name="sortedItems">
        /// An <see cref="IEnumerable{T}"/> to provide item orders.
        /// </param>
        private void InternalSort(IEnumerable<T> sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();

            foreach (var item in sortedItemsList)
            {
                Move(IndexOf(item), sortedItemsList.IndexOf(item));
            }
        }
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Observable Collection demo