DD.WellWorkover.Cloud/AsbCloudInfrastructure/Background/OrderedList.cs
2023-10-08 19:45:21 +05:00

42 lines
1.2 KiB
C#

using System.Linq;
namespace System.Collections.Generic
{
public class OrderedList<T>: IEnumerable<T>, ICollection<T>
where T : notnull
{
private readonly List<T> list = new List<T>();
private readonly Func<T, object> keySelector;
private readonly bool isDescending = false;
private IOrderedEnumerable<T> OrdredList => isDescending
? list.OrderByDescending(keySelector)
: list.OrderBy(keySelector);
public int Count => list.Count;
public bool IsReadOnly => false;
public OrderedList(Func<T, object> keySelector, bool isDescending = false)
{
this.keySelector = keySelector;
this.isDescending = isDescending;
}
public void Add(T item) => list.Add(item);
public void Clear()=> list.Clear();
public bool Contains(T item)=> list.Contains(item);
public void CopyTo(T[] array, int arrayIndex)=> list.CopyTo(array, arrayIndex);
public bool Remove(T item)=> list.Remove(item);
public IEnumerator<T> GetEnumerator() => OrdredList.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}