In C#, Array.Sort rearranges the elements of an array into sorted order. The default is ascending (smallest to largest for numbers, alphabetical for strings with the default comparer).
Sorting happens in place: the same array object is reordered; Sort does not return a new array. After Sort, BinarySearch becomes valid.
This is a static member of System.Array. You call it with the class name, such as Array.Sort(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.
Syntax
Pass the array to the static method. Optionally pass a range or comparer.
C#
Array.Sort(nums);
Array.Sort(names);
Call
What it does
Array.Sort(nums)
Sorts the whole array ascending.
Array.Sort(nums, 1, 3)
Sorts 3 elements starting at index 1.
For descending order, sort ascending then Reverse, or pass a custom comparer.
Library declarations
C#
public static void Sort(Array array);
public static void Sort<T>(T[] array);
Parameters
Parameter
Description
array
Required. Array to reorder in place.
index / length
Optional. Sort only a slice.
Return Value
Return Value
Description
void
The array is sorted in place.
C# Examples
Example 1 — Sort Integers Ascending
Reorder an unsorted int array.
C#
using System;
class Program {
static void Main() {
int[] nums = { 40, 10, 30, 20 };
Array.Sort(nums);
Console.WriteLine(string.Join(", ", nums));
}
}
Output
10, 20, 30, 40
How It Works
In place. The same nums object is reordered.
Ascending. Smallest value moves to index 0.
All elements. This overload sorts the full length.
Ready for BinarySearch. Order is now valid for binary search.
Example 2 — Sort Names Alphabetically
Sort a string array with the default comparer.
C#
using System;
class Program {
static void Main() {
string[] names = { "Dan", "Ann", "Cara", "Bob" };
Array.Sort(names);
Console.WriteLine(string.Join(", ", names));
}
}
Output
Ann, Bob, Cara, Dan
How It Works
Default order. Strings sort alphabetically (culture-aware by default for some overloads).
Ann first. A comes before B, C, D.
Original lost. If you need the old order, Clone first.
Case. Letter case can affect ordering depending on comparer.
Example 3 — Sort Only a Slice
Leave the first element fixed; sort the rest.
C#
using System;
class Program {
static void Main() {
int[] nums = { 99, 40, 10, 30 };
Array.Sort(nums, 1, 3);
Console.WriteLine(string.Join(", ", nums));
}
}
Output
99, 10, 30, 40
How It Works
Start at 1. Index 0 stays 99.
Length 3. Indexes 1–3 are sorted.
Local order. 10, 30, 40 replace 40, 10, 30.
Partial tools. Useful when a header slot must stay put.
Frequently Asked Questions
No. It reorders the existing array in place.
Ascending according to the default comparer for the element type.
Sort ascending then Array.Reverse, or pass a reverse comparer.
BinarySearch requires sorted data. Unsorted arrays give undefined BinarySearch results.