How to Sort an Array in C# With Examples?

In this C# tutorial explains, how to sort an array in C# with examples. I will show you different ways to sort a C# array. We will also cover, how to sort an array in c# in descending order. and how to sort an array in c# in ascending order. Finally, I will show you, how to sort an array alphabetically in c#.

Sort an Array in C#

Here are 4 different ways to sort an array in C#.

1. Using the Array.Sort() Method

The simplest way to sort an array in C# is by using the built-in Array.Sort() method.

using System;

namespace SortingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 23, 1, 4, 67, 34, 9 };
            
            Array.Sort(numbers);

            foreach (int num in numbers)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Once you run the code, you can see the output below:

sort an array in c#

This is how to sort an array using the Array.Sort() method.

2. Sorting an Array of Strings

The Array.Sort() method also works on string arrays and sorts them in lexicographical (alphabetical) order in C#.

string[] fruits = { "Banana", "Apple", "Cherry", "Date" };

Array.Sort(fruits);

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

3. Using LINQ to Sort Arrays

Language Integrated Query (LINQ) provides a more flexible approach to sorting arrays. With LINQ, you can sort C# arrays based on specific criteria.

To use LINQ, add using System.Linq; at the top of your code.

using System;
using System.Linq;

namespace LinqSortingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 23, 1, 4, 67, 34, 9 };
            
            var sortedNumbers = numbers.OrderBy(n => n).ToArray();

            foreach (int num in sortedNumbers)
            {
                Console.WriteLine(num);
            }
        }
    }
}

Using LINQ, you can also sort in descending order:

var sortedNumbersDesc = numbers.OrderByDescending(n => n).ToArray();

4. Custom Sorting Using IComparer

For more control over the sorting process, especially when dealing with complex types, you can use IComparer. Let’s consider an example with an array of Person objects, where you want to sort based on age.

using System;
using System.Collections.Generic;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class AgeComparer : IComparer<Person>
{
    public int Compare(Person x, Person y)
    {
        return x.Age.CompareTo(y.Age);
    }
}

namespace CustomSortingExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] people = {
                new Person { Name = "John", Age = 28 },
                new Person { Name = "Doe", Age = 22 },
                new Person { Name = "Anna", Age = 25 }
            };
            
            Array.Sort(people, new AgeComparer());

            foreach (Person person in people)
            {
                Console.WriteLine($"{person.Name}, {person.Age} years old");
            }
        }
    }
}

How to sort an array in c# in ascending order?

Let us see, how to sort an array in c# in ascending order.

Sorting an array in ascending order in C# is straightforward using the built-in Array.Sort() method.

Example:

using System;

namespace ArraySortingExample
{
    class Program
    {
        static void Main()
        {
            int[] numbers = { 45, 12, 88, 33, 9 };

            Array.Sort(numbers);

            foreach (int num in numbers)
            {
                Console.WriteLine(num);
            }
        }
    }
}

You can see the output like below:

how to sort an array in c# in ascending order

This is how to sort an array in c# in ascending order.

How to sort an array in c# in descending order?

Let us see how to sort an array in c# in descending order.

To sort an array in descending order in C#, you can use the Array.Sort() method followed by the Array.Reverse() method.

Example:

using System;

namespace ArraySortingExample
{
    class Program
    {
        static void Main()
        {
            int[] numbers = { 45, 12, 88, 33, 9 };

            Array.Sort(numbers);
            Array.Reverse(numbers);

            foreach (int num in numbers)
            {
                Console.WriteLine(num);
            }
        }
    }
}

This is how to sort an array in c# in descending order.

How to sort an array alphabetically in c#?

Let us check an example of how to sort an array alphabetically in c#.

When sorting arrays of strings, the Array.Sort() method automatically uses lexicographical (alphabetic) order in C#.

Example:

using System;

namespace ArraySortingExample
{
    class Program
    {
        static void Main()
        {
            string[] names = { "Oliver", "Emma", "Liam", "Ava", "Sophia" };

            Array.Sort(names);

            foreach (string name in names)
            {
                Console.WriteLine(name);
            }
        }
    }
}

Once you run the code, you can see the output below:

Ava
Emma
Liam
Oliver
Sophia

You can see the output below in the screenshot.

how to sort an array alphabetically in c#

This is how to sort an array alphabetically in c#.

Conclusion

Here, I have explained, how to sort an array in C# using various methods. With examples, I have explained the below examples:

  • How to sort an array in c# in descending order?
  • How to sort an array in c# in ascending order?
  • How to sort an array alphabetically in c#?
  • How to sort an array in c# without sort() method?

You may also like: