Within the .NET framework, you will sometimes find methods (such as Sorting) that have one or more overloads that accept additional types (interfaces or delegates) to extend their behavior. Unlike C ++, .NET does not have the same approach as STL for composite algorithms.
In the case of List.Sort there are two overloads that might be useful:
List.Sort( IComparer<T> comparer ) // and List.Sort( Comparison<T> comparison ) // .NET 4.0 and up
The first overload takes an instance of a type that implements IComparer<T> - an interface with a single Compare method. The second overload is only available if you are using .NET 4.0 or later โ it accepts a delegate (or lambda expression) that provides comparison semantics.
If you can, the second overload is much easier to use:
intList.Sort( (a,b) => YourCompare(a,b) );
To use the first overload, you must create a class or structure that implements IComparer<T> :
public sealed class YourComparer : IComparer<YourType> { int Compare( YourType a, YourType b ) { ... } } intList.Sort( new YourComparer() );
If you do not want to modify the collection yourself, but only sort its elements and operate on them as a new sequence, you can use LINQ OrderBy :
intList.OrderBy( x => ... ).ToArray()
<h / "> To answer the second part of your question, if you want to sort only a certain range of a specific collection, you will have to use the Sort( int, int, IComparer<T> ) overload Sort( int, int, IComparer<T> ) .