
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - Sort() Method
The C# Array Sort() method sorts the array elements in ascending order. It provides multiple overloaded methods for sorting based on various parameters, such as the entire array, a range within the array, or a custom comparer. Let's see the syntax and its definition below.
Syntax
Following are the syntax of the C# Array Sort() method −
Sort Entire Array
Sorts the all elements of an array in ascending order −
Array.Sort(array);
Sort Using the Range
Sorts the elements in a range starting from the given index and spanning length element −
Array.Sort(array, index, length);
Sort Using a Custom Comparer
Sorts the elements of array using the Icomparer function −
Array.Sort(array, comparer);
Sort a Range Using a Custom Comparer
Sorts a specified range of elements in an array using a custom IComparer function −
Array.Sort(array, index, length, comparer);
Parameters
This method accepts the following parameters according to the all-overloaded method −
- array: It is a mandatory parameter, which is an array for sorting.
- index: It is an optional parameter, represent starting index of the portion to Sort
- length: It is an optional parameter, represent number of elements in the range to sort.
- comparer: The IComparer implementation to use when comparing elements.
Return value
This method does not return any value.
Example 1: Sort the Entire Array
Let us crate a basic example of the Sort() method to Sort the entire integer array −
using System; class Program { static void Main() { int[] numbers = { 4, 5, 8, 7, 3, 1, 2}; // Sort the array Array.Sort(numbers); Console.WriteLine(string.Join(", ", numbers)); } }
Output
Following is the output −
1, 2, 3, 4, 5, 7, 8
Example 2: Sort Using a Range
Let us see another example of the Sort() method to Sort the elements of the array in a range −
using System; class Program { static void Main() { int[] numbers = { 4, 5, 8, 7, 3, 1, 2}; // Sort the array Array.Sort(numbers, 2, 5); Console.WriteLine(string.Join(", ", numbers)); } }
Output
Following is the output −
4, 5, 1, 2, 3, 7, 8
Example 3: Custom Sorting with IComparer
This is another, example of the Sort() method. Here, we define our sorting logic by implementing the Icomparer interface −
using System; using System.Collections; class DescendingComparer : IComparer { public int Compare(object x, object y) { int intX = (int)x; int intY = (int)y; // Reverse the comparison to sort in descending order return intY.CompareTo(intX); } } class Program { static void Main() { int[] numbers = { 5, 3, 1, 4, 2 }; // Sort in descending order using a custom comparer Array.Sort(numbers, new DescendingComparer()); Console.WriteLine(string.Join(", ", numbers)); } }
Output
Following is the output −
5, 4, 3, 2, 1
Example 4: Sort Using Case-Insensitive Comparer
The following example shows how to sort the values in an array in range using the custom comparer that reverses the sort order −
using System; using System.Collections; public class ReverseComparer: IComparer { // Call CaseInsensitiveComparer.Compare with the parameters reversed. public int Compare(Object x, Object y) { return (new CaseInsensitiveComparer()).Compare(y, x); } } public class Example { public static void Main() { // Create and initialize a new array. String[] words = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" }; // Instantiate the reverse comparer. IComparer revComparer = new ReverseComparer(); // Display the values of the array. Console.WriteLine("The original order of elements in the array:"); DisplayValues(words); // Sort a section of the array using the reverse case-insensitive comparer. Array.Sort(words, 1, 3, revComparer); Console.WriteLine("After sorting elements 1-3 by using the reverse case-insensitive comparer:"); DisplayValues(words); } public static void DisplayValues(String[] arr) { for (int i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++) { Console.WriteLine(" [{0}] : {1}", i, arr[i]); } Console.WriteLine(); } }
Output
Following is the output −
The original order of elements in the array: [0] : The [1] : QUICK [2] : BROWN [3] : FOX [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog After sorting elements 1-3 by using the reverse case-insensitive comparer: [0] : The [1] : QUICK [2] : FOX [3] : BROWN [4] : jumps [5] : over [6] : the [7] : lazy [8] : dog