C# Array Sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

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);
CallWhat 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

ParameterDescription
arrayRequired. Array to reorder in place.
index / lengthOptional. Sort only a slice.

Return Value

Return ValueDescription
voidThe 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));
    }
}

How It Works

  1. In place. The same nums object is reordered.
  2. Ascending. Smallest value moves to index 0.
  3. All elements. This overload sorts the full length.
  4. 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));
    }
}

How It Works

  1. Default order. Strings sort alphabetically (culture-aware by default for some overloads).
  2. Ann first. A comes before B, C, D.
  3. Original lost. If you need the old order, Clone first.
  4. 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));
    }
}

How It Works

  1. Start at 1. Index 0 stays 99.
  2. Length 3. Indexes 1–3 are sorted.
  3. Local order. 10, 30, 40 replace 40, 10, 30.
  4. 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.
Yes. Overloads accept an index and length.

Next: TrueForAll()

Return true only if every element matches a predicate with System.Array.

TrueForAll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful