How about using a List<> and calling the Sort method?
Not an extension but try this
public class SortedList<T>: List<T>
{
public SortedList(): base()
{
}
public SortedList(IEnumerable<T> collection): base(collection)
{
}
public SortedList(int capacity)
: base(capacity)
{
}
public void AddSort(T item)
{
base.Add(item);
this.Sort();
}
}
It's only a starting point, but adds a new method AddSort.
An extension method would be used to alter the List<>.Add method and call the sort on the end of it.
Using extension method
Place the following in a namespace accessible by your code:
public static class ListExtension
{
public static void AddSort<T>(this List<T> list, T item)
{
list.Add(item);
list.Sort();
}
}
You can the use code such as:
List<int> newList = List<int>();
newList.AddSort(6);
newList.AddSort(4);
newList.AddSort(3);
And the values will be:
newList[0] == 3
newList[1] == 4
newList[3] == 6
You can also just use newList.Add and the list is then sorted when you call newList.AddSort