TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Array.Sort Examples

These C# example programs demonstrate the Array.Sort method. Array.Sort has many overloads.

Array.Sort orders elements in an array.

It provides a simple way to sort the elements in an array. It handles different types of elements, including strings and ints. And it has an efficient implementation.

Example. To start, this program allocates an array of four integers that are not already sorted in any way. Next, it calls the Array.Sort method. This sorts the elements in-place, meaning you do not need to acquire a new array reference.

Int Array

Then: We enumerate the integers in the sorted array, printing them to the console.

Console.Write

C# program that uses simple Array.Sort overload

using System;

class Program
{
    static void Main()
    {
	// Simple sort call.
	int[] values = { 4, 7, 2, 0 };
	Array.Sort(values);
	foreach (int value in values)
	{
	    Console.Write(value);
	    Console.Write(' ');
	}
	Console.WriteLine();
    }
}

Output

0 2 4 7

Array reference. This program creates an array of four integer elements. Then, it references the newly-created int array as an Array. We can do this because the int[] type is derived from the Array type.

Arrays

Next: This Array reference is passed to the Array.Sort method, which can sort the elements.

And: Before program completion, we display the sorted elements in the Array reference.

C# program that uses Array reference type

using System;

class Program
{
    static void Main()
    {
	// Sort Array reference.
	int[] values = { 4, 7, 2, 0 };
	Array array = values;
	Array.Sort(array);
	foreach (int value in array)
	{
	    Console.Write(value);
	    Console.Write(' ');
	}
	Console.WriteLine();
    }
}

Output

0 2 4 7

Keys, values. In this example, we declare two arrays (keys and values) and set the elements in them. Next, we call the Array.Sort overload that accepts two arrays: the first array is the keys array and the second, the values array.

Finally: The program prints out the keys array in its sorted order, and the values array in its sorted order.

C# program that sorts keys and values

using System;

class Program
{
    static void Main()
    {
	// Sort keys and values.
	int[] keys = { 4, 7, 2, 0 };
	int[] values = { 1, 2, 3, 4 };
	Array.Sort(keys, values);
	foreach (int key in keys)
	{
	    Console.Write(key);
	    Console.Write(' ');
	}
	Console.WriteLine();
	foreach (int value in values)
	{
	    Console.Write(value);
	    Console.Write(' ');
	}
	Console.WriteLine();
    }
}

Output

0 2 4 7
4 3 1 2

Range. This program creates an integer array upon the managed heap. Then, it calls the Array.Sort method with three arguments: the array reference (int[]), and also the starting index (0) and the number of elements to sort past that index.

Note: This program uses 0, 3 to sort only the first three elements. It sorts a range of elements.

C# program that sorts range of elements

using System;

class Program
{
    static void Main()
    {
	// Sort range of elements.
	int[] values = { 4, 7, 2, 0 };
	Array.Sort(values, 0, 3);
	foreach (int value in values)
	{
	    Console.Write(value);
	    Console.Write(' ');
	}
	Console.WriteLine();
    }
}

Output

2 4 7 0

You can see in the results that the first three elements are sorted in ascending numeric order. The fourth element 0 is still at the end because it was not included in the sorting operation.

Summary. The Array.Sort method has many overloads. The basic approach to calling the Array.Sort method is demonstrated. You can modify the arguments to invoke the more sophisticated versions of the method.

Finally: Other types such as the List type provide sorting methods that internally call Array.Sort.

Sort List


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf