How to sort a list in C# | List.Sort() Method Set -1
Last Updated :
12 Feb, 2019
List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of this method as follows:
- Sort(IComparer<T>)
- Sort(Int32, Int32, IComparer)
- Sort()
- Sort(Comparison<T>)
Here, we will discuss the first two methods.
Sort(IComparer<T>) Method
This method is used to sort the elements in the entire List<T> using the specified comparer.
Syntax:
public void Sort (System.Collections.Generic.IComparer<T> comparer);
Here, the comparer is the IComparer<T> implementation to use when comparing elements, or null to use the default comparer Default.
Exceptions:
- InvalidOperationException: If the comparer is null, and the default comparer Default cannot find the implementation of the IComparable<T> generic interface or the IComparable interface for type T.
- ArgumentException: If the implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself.
Example 1:
csharp
// C# program to demonstrate the concept of
// List<T>.Sort(IComparer <T>) method
using System;
using System.Collections.Generic;
class GFG : IComparer<int>
{
public int Compare(int x, int y)
{
if (x == 0 || y == 0)
{
return 0;
}
// CompareTo() method
return x.CompareTo(y);
}
}
public class geek
{
// Main Method
public static void Main()
{
// List initialize
List<int> list1 = new List<int>
{
// list elements
1,5,6,2,4,3
};
Console.WriteLine("Original List");
foreach(int g in list1)
{
// Display Original List
Console.WriteLine(g);
}
// "gg" is the object oif class GFG
GFG gg = new GFG();
Console.WriteLine("\nSort with a comparer:");
// use of List<T>.Sort(IComparer<T>)
// method. The comparer is "gg"
list1.Sort(gg);
foreach( int g in list1 )
{
// Display sorted list
Console.WriteLine(g);
}
}
}
Output:
Original List
1
5
6
2
4
3
Sort with a comparer:
1
2
3
4
5
6
Example 2:
csharp
// C# program to demonstrate the concept of
// List<T>.Sort(IComparer <T>) method
using System;
using System.Collections.Generic;
class GFG : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return 0;
}
// "CompareTo()" method
return x.CompareTo(y);
}
}
public class geek
{
// Main Method
public static void Main()
{
List<string> list1 = new List<string>();
// list elements
list1.Add("A");
list1.Add("I");
list1.Add("G");
list1.Add("B");
list1.Add("E");
list1.Add("H");
list1.Add("F");
list1.Add("C");
list1.Add("J");
Console.WriteLine("Original List");
// Display Original List
Display(list1);
// "gg" is the object
GFG gg = new GFG();
Console.WriteLine("\nSort with a comparer:");
// sort the list with a
// specified comparer "gg"
list1.Sort(gg);
// Display sorted List
Display(list1);
Console.WriteLine("\nBinarySearch and Insert D");
// Binary Search for "D"
// using List.BinarySearch(T) method
int index = list1.BinarySearch("D");
if (index < 0)
{
// range++;
list1.Insert(~index, "D");
}
// Display the List after
// inserting "D"
Display(list1);
}
// Display function
public static void Display(List<string> list)
{
foreach( string g in list )
{
Console.WriteLine(g);
}
}
}
Output:
Original List
A
I
G
B
E
H
F
C
J
Sort with a comparer:
A
B
C
E
F
G
H
I
J
BinarySearch and Insert D
A
B
C
D
E
F
G
H
I
J
List<T>.Sort(Int32, Int32, IComparer<T>) Method
This method is used to sort the elements in a range of elements in List<T> using the specified comparer.
Syntax:
public void Sort(int index, int len, IComparer<T> comparer)
Parameters:
index : It is the zero-based starting index of the range in which sort will happen.
len : It is the length of the range.
comparer : When comparing elements then use the IComparer implementation or null to use the default comparer Default.
Exceptions:
- ArgumentOutOfRangeException : If indexor len is less than 0.
- ArgumentException : If index and count do not specify a valid range in the List.
- InvalidOperationException : If comparer is null.
Example:
csharp
// C# program to demonstrate the use of
// List<T>.Sort(Int32, Int32, IComparer<T>)
// Method
using System;
using System.Collections.Generic;
class GFG : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return 0;
}
// "CompareTo()" method
return x.CompareTo(y);
}
}
public class geek
{
public static void Main()
{
List<string> list1 = new List<string>();
// list elements
list1.Add("C++");
list1.Add("Java");
list1.Add("C");
list1.Add("Python");
list1.Add("HTML");
list1.Add("CSS");
list1.Add("Scala");
list1.Add("Ruby");
list1.Add("Perl");
int range = 4;
Console.WriteLine("Original List");
// Display Original List
Display(list1);
// "gg" is the object
GFG gg = new GFG();
Console.WriteLine("\nSort a range with comparer:");
// sort the list within a
// range of index 1 to 4
// where range = 4
list1.Sort(1, range, gg);
// Display sorted List
Display(list1);
Console.WriteLine("\nBinarySearch and Insert Dart");
// Binary Search and storing
// index value to "index"
int index = list1.BinarySearch(0, range,
"Dart", gg);
if (index < 0)
{
list1.Insert(~index, "Dart");
range++;
}
// Display the List
// after inserting "Dart"
Display(list1);
}
// Display function
public static void Display(List<string> list)
{
foreach(string g in list)
{
Console.WriteLine(g);
}
}
}
Output:
Original List
C++
Java
C
Python
HTML
CSS
Scala
Ruby
Perl
Sort a range with comparer:
C++
C
HTML
Java
Python
CSS
Scala
Ruby
Perl
BinarySearch and Insert Dart
C++
C
Dart
HTML
Java
Python
CSS
Scala
Ruby
Perl
Reference:
Similar Reads
How to sort a list in C# | List.Sort() Method Set -2 List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of th
5 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1 Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to sort an Array in C# | Array.Sort() Method Set â 2 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method
9 min read
How to sort an Array in C# | Array.Sort() Method | Set â 4 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array) Method This method sorts the elements in an entire one-dimensional array using the IComparable implementation of ea
5 min read
How to sort an Array in C# | Array.Sort() Method Set â 3 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, IComparer) Method Sort(Array, Array, IComparer) Method Sort(Array, Array) Method Sort(Array, IComparer) Method This
6 min read
How to sort an Array in C# | Array.Sort() Method | Set â 5 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) Method This method sorts a pair of array objects based on the
7 min read