mirror of
https://github.com/Tribufu/ServerManagers
synced 2026-08-10 21:21:06 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
|
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace ServerManagerTool.Common.Model
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed and allows sorting.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the collection.</typeparam>
|
||||
public class SortableObservableCollection<T> : ObservableCollection<T>
|
||||
{
|
||||
public SortableObservableCollection()
|
||||
{
|
||||
}
|
||||
|
||||
public SortableObservableCollection(List<T> list)
|
||||
: base(list)
|
||||
{
|
||||
}
|
||||
|
||||
public SortableObservableCollection(IEnumerable<T> collection)
|
||||
: base(collection)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
InternalSort(Items.OrderBy(keySelector, comparer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the items of the collection in descending 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 SortDescending<TKey>(Func<T, TKey> keySelector)
|
||||
{
|
||||
InternalSort(Items.OrderByDescending(keySelector));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorts the items of the collection in descending 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 SortDescending<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
InternalSort(Items.OrderByDescending(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue