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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » ObservableCollectionScreenshots 
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
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.