How to Sort an Array in C#: A Complete Guide

Category > CSHARP || Published on : Tuesday, March 14, 2023 || Views: 231 || C# sorting Array.Sort Array.Reverse IComparable LINQ.


Sorting an array is a fundamental task in programming, and C# provides several ways to sort an array. In this article, we will discuss the most commonly used methods for sorting an array in C#. We will cover how to sort an array in ascending or descending order, how to sort an array of objects, and using LINQ for sorting.

Sorting an array is a common task in programming. In C#, there are several ways to sort an array. In this article, we will discuss the most commonly used methods for sorting an array in C#.

Sorting an Array in C#

  1. Array.Sort Method The easiest way to sort an array in C# is by using the Array.Sort method. This method sorts the elements of an array in ascending order. The syntax of the Array.Sort method is as follows:
Array.Sort(array);

The parameter of the Array.Sort method is the array that you want to sort. Here is an example code:

int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
Array.Sort(numbers);

This code will sort the numbers array in ascending order.

  1. Array.Reverse Method If you want to sort an array in descending order, you can use the Array.Reverse method. This method reverses the order of the elements in an array. The syntax of the Array.Reverse method is as follows:
Array.Reverse(array);

Here is an example code:

int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
Array.Sort(numbers);
Array.Reverse(numbers);

This code will sort the numbers array in ascending order and then reverse the order of the elements.

  1. Sorting an Array of Objects If you have an array of objects, you can sort the array by implementing the IComparable interface. The IComparable interface defines a method called CompareTo that compares the current object with another object of the same type. Here is an example code:
class Person : IComparable<Person>
{
    public string Name { get; set; }
    public int Age { get; set; }

    public int CompareTo(Person other)
    {
        return this.Age.CompareTo(other.Age);
    }
}

Person[] people = new Person[]
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Charlie", Age = 20 }
};

Array.Sort(people);

This code will sort the people array in ascending order of age.

  1. Sorting an Array using LINQ LINQ (Language-Integrated Query) is a powerful feature of C# that allows you to query and manipulate data. You can use the OrderBy method of the LINQ library to sort an array. Here is an example code:
int[] numbers = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
var sortedNumbers = numbers.OrderBy(x => x);

This code will sort the numbers array in ascending order and store the sorted result in the sortedNumbers variable.

Conclusion

In this article, we discussed the most commonly used methods for sorting an array in C#. The Array.Sort method is the easiest and most efficient way to sort an array in ascending order. If you want to sort an array in descending order, you can use the Array.Reverse method. If you have an array of objects, you can sort the array by implementing the IComparable interface. Finally, you can use the OrderBy method of the LINQ library to sort an array.