TechieClues TechieClues
Updated date Aug 22, 2022
In this blog, we will see how to sort an array in C#. Arrays are used to store more than one value in a single variable. If you want to declare an array in C#, then you have to define the variable type with square brackets ([]). 
  • 7.4k
  • 0
  • 0

How to Sort an Array using Array.Sort() and LINQ in C#?

In this blog, we will see how to sort an array in C#. Arrays are used to store more than one value in a single variable. If you want to declare an array in C#, then you have to define the variable type with square brackets ([]). 

Types of Array in C#:

  • Single-dimensional array
  • Multidimensional array
  • Jagged array

Array.Sort():

Array.Sort()  allows you to sorts the items of an array in place. You can use this method to sort the items in many ways.

Sort an array in Ascending Order:

The below example shows how to sort array items in ascending order using Array.Sort() method.

class Program
{
	static void Main(string[] args)
	{
		string[] cars = { "BMW", "Toyoto", "Audi", "KIA", "Tesla", "Land Rover", "Suzuki" };

        // Sort an array in ascending order
		Array.Sort(cars); 

		foreach(var i in cars)
		{
			Console.WriteLine(i);
		}
		Console.ReadKey();
	}
}

Output:

Audi
BMW
KIA
Land Rover
Suzuki
Tesla
Toyoto

Sort an array in Descending Order:

We don't have any inbuilt method to sort an array in descending order, we have to change the logic and use CompareTo() along with Array.Sort() method to achieve this as shown below. The below example sorts an array in descending order,

class Program
{
	static void Main(string[] args)
	{
		string[] cars = { "BMW", "Toyoto", "Audi", "KIA", "Tesla", "Land Rover", "Suzuki" };          

        // Sort an array in descending order
		Array.Sort<string>(cars,
			   new Comparison<string>(
					   (i1, i2) => i2.CompareTo(i1)
			   ));

		foreach (var i in cars)
		{
			Console.WriteLine(i);
		}
		Console.ReadKey();
	}
}

Output:

Toyoto
Tesla
Suzuki
Land Rover
KIA
BMW
Audi

Array.Reverse() : 

The Reverse() method is used to reverses the sequence of the elements in the entire one-dimensional Array. The below example reverses the sequence of the elements in an array.

class Program
{
	static void Main(string[] args)
	{
		string[] cars = { "BMW", "Toyoto", "Audi", "KIA", "Tesla", "Land Rover", "Suzuki" };

		Array.Reverse(cars);

		foreach (var i in cars)
		{
			Console.WriteLine(i);
		}
		Console.ReadKey();
	}
}

Output:

Suzuki
Land Rover
Tesla
KIA
Audi
Toyoto
BMW

Sorting an Array using LINQ:

We can also use LINQ to sort array elements. The below example shows how to sort array elements in ascending and descending order using LINQ.

class Program
{
	static void Main(string[] args)
	{
		string[] cars = { "BMW", "Toyoto", "Audi", "KIA", "Tesla", "Land Rover", "Suzuki" };            
		
		// Sort array elements using LINQ - Ascending Order
		var sortedValuesAsc = from car in cars
						orderby car
						select car;

		// Sort array elements using LINQ - Descending Order
		var sortedValuesDesc = from car in cars
						   orderby car descending
						   select car;

		Console.WriteLine("----Ascending Order----");
		Array.ForEach<string>(sortedValuesAsc.ToArray<string>(), x => Console.WriteLine(x));
		Console.WriteLine("----Descending Order----");
		Array.ForEach<string>(sortedValuesDesc.ToArray<string>(), y => Console.WriteLine(y));
		Console.ReadKey();
	}
}

Output:

----Ascending Order----
Audi
BMW
KIA
Land Rover
Suzuki
Tesla
Toyoto

----Descending Order----
Toyoto
Tesla
Suzuki
Land Rover
KIA
BMW
Audi

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!