using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace AdvancementVoyage.Magic.Utility
{
///
/// An observable collection that can be sorted.
///
/// The type of item contained in the observable collection.
[Serializable]
internal sealed class SortableObservableCollection : ObservableCollection
{
///
/// Initializes a new instance of the SortableObservableCollection
/// class.
///
public SortableObservableCollection()
: base() { }
///
/// Initializes a new instance of the SortableObservableCollection class
/// that contains elements copied from the specified collection.
///
///
/// The collection from which the elements are copied.
///
///
/// The collection parameter cannot be a null reference.
///
public SortableObservableCollection(IEnumerable collection)
: base(collection) { }
///
/// Initializes a new instance of the SortableObservableCollection
/// class that contains elements copied from the specified list.
///
///
/// The list from which the elements are copied.
///
///
/// The list parameter cannot be a null reference.
///
public SortableObservableCollection(List list)
: base(list) { }
///
/// Sorts the items of the collection in ascending order according to a key.
///
///
/// The type of the key returned by .
///
///
/// A function to extract a key from an item.
///
public void Sort(Func keySelector)
{
this.InternalSort(Items.OrderBy(keySelector));
}
///
/// Sorts the items of the collection in ascending order according to a key.
///
///
/// The type of the key returned by .
///
///
/// A function to extract a key from an item.
///
///
/// An to compare keys.
///
public void Sort(Func keySelector, IComparer comparer)
{
this.InternalSort(Items.OrderBy(keySelector, comparer));
}
///
/// Moves the items of the collection so that their orders are the same as those of the items provided.
///
///
/// An to provide item orders.
///
private void InternalSort(IEnumerable sortedItems)
{
var sortedItemsList = sortedItems.ToList();
foreach (var item in sortedItemsList)
{
Move(IndexOf(item), sortedItemsList.IndexOf(item));
}
}
}
}