Why not just sort the list in place using the Sort() instance method; then you can add elements to it later if you want:
List<string> items = GetSomeItems(); items.Sort();
Or use an ordered collection, like a binary search tree. SortedSet<T> may match the bill, depending on your needs.
Solution suggested by others:
items = items.OrderBy(item => item).ToList();
... creates a new list with original items in a new order. This is only useful if you need to keep the original order for some other purpose; it's rather more useless for memory than sorting a list in place.
As for understanding the error, it's simple: List<T> not a subtype of IOrderedEnumerable<T> , so there is no implicit conversion of links between them. The explicit offer that the compiler offers will satisfy the compiler, but it will not work at runtime because the object returned by OrderBy<T> does not inherit from List<T> .
EDIT
Example List<T>.Sort(Comparison<T>) , assuming that the MyType type has a Key property of some type of type T, where T : IComparable<T> :
List<MyType> items = GetSomeItems(); items.Sort((a, b) => a.Key.CompareTo(b.Key));
phoog Feb 14 2018-12-12T00: 00Z
source share