Search⌘ K
AI Features

Sorting Collections

Explore sorting techniques for collections in C#. Understand how to sort List<T> manually and why Stack<T> and Queue<T> cannot be sorted. Discover automatically sorted collections and how to choose the right one based on performance and memory needs. Learn about specialized collections like BitArray and LinkedList<T> for efficient data manipulation.

Sorting with List<T>

A List<T> class can be sorted by manually calling its Sort method (but remember that the indexes of each item will change). Manually sorting a list of string values or other built-in types will work without extra effort on our part, but if we create a collection of our own type, then that type must implement an interface named IComparable.

No sorting for Stack<T> and Queue<T>

A Stack<T> or Queue<T> collection cannot be sorted ...