C# Sort String Array

You need to sort your List or array of strings using the C# programming language. You have a small or medium number of strings, meaning you don't need to deal with edge cases. The C# language and .NET Framework has several collection sorting methods and also there is LINQ query syntax. This document benchmarks and demonstrates the sort methods, such as Array.Sort.

Example string arrays

Here we see the example string[] array that you have in your C# program. The name "Zimbabwe" comes after the name "America", so the output array must be ordered with America first and finally Zimbabwe.
Unsorted

Zimbabwe
America
India
Germany
China
Ukraine

Sorted

America
China
India
Germany
Ukraine
Zimbabwe

Sort in place

Here we see how you can call the static Array.Sort method and use it to sort a string array in place. The result is an alphabetical sort. This console program demonstrates how to use the Array.Sort method.
Program that uses Array.Sort [C#]

using System;

class Program
{
    static void Main()
    {
 string[] a = new string[]
 {
     "Egyptian",
     "Indian",
     "American",
     "Chinese",
     "Filipino",
 };
 Array.Sort(a);
 foreach (string s in a)
 {
     Console.WriteLine(s);
 }
    }
}

Output

American
Chinese
Egyptian
Filipino
Indian

Sort strings with LINQ

Here we see how to take a string[] array and use a LINQ query expression to order its contents alphabetically. Note that we are ordering the strings, not the letters in the strings. You can see that the orderby keyword results in the same output as the Array.Sort method.
Program that uses LINQ [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
 string[] a = new string[]
 {
     "Indonesian",
     "Korean",
     "Japanese",
     "English",
     "German"
 };
 var sort = from s in a
     orderby s
     select s;

 foreach (string c in sort)
 {
     Console.WriteLine(c);
 }
    }
}

Output

English
German
Indonesian
Japanese
Korean
Note on the example code. When you use the query expression in LINQ, it returns an IEnumerable collection. This just means it is a collection that you have to enumerate (loop over) to get.

Sort strings in reverse (LINQ)

Here we sort strings from Z-A instead of from A-Z. This is called reverse alphabetic order. LINQ here is used with a query expression that is an enumeration of the original strings ordered from Z-A.
Program that uses LINQ descending [C#]

using System;
using System.Linq;

class Program
{
    static void Main()
    {
 string[] a = new string[]
 {
     "French",
     "Italian",
     "European",
     "Irish",
     "Vietnamese"
 };
 var desc = from s in a
     orderby s descending
     select s;

 foreach (string c in desc)
 {
     Console.WriteLine(c);
 }
    }
}

Output

Vietnamese
Italian
Irish
French
European
Description. This example is the same as in the previous example, except use different strings, and uses the descending keyword. Ascending means to go from lowest to highest (A-Z), while descending means to go from highest to lowest (Z-A). You can find more information on the descending contextual keyword.
Ascending KeywordDescending Keyword

Sort List

List<string> indicates a generic List collection of strings. Internally, it is stored as an array, but it is not an array type in the C# language. Therefore, you have to use its separate Sort method. Also, you can use LINQ with the exact syntax on List as used on arrays. This is because both arrays and Lists implement the IEnumerable interface.
Sort List Method
Program that uses List [C#]

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<string> l = new List<string>()
 {
     "Australian",
     "Mongolian",
     "Russian",
     "Austrian",
     "Brazilian"
 };
 l.Sort();
 foreach (string s in l)
 {
     Console.WriteLine(s);
 }
    }
}

Output

Australian
Austrian
Brazilian
Mongolian
Russian
Tip

Review

We can look at what MSDN and other sites say about the sort methods I showed in the examples. I reference MSDN as well as other articles on Dot Net Perls.
Array.Sort. This method sorts the elements in the received array. It uses the IComparable interface implementation IComparable uses the CompareTo method.
IComparable Example With CompareToArray.Sort Examplemsdn.microsoft.comList(T).Sort. This is a generic class method. It must have its type specified at compile-time. It can receive a delegate method, which is not covered in this article. MSDN: "This method uses Array.Sort which uses the QuickSort algorithm." This means this method is equivalent to Array.Sort.
msdn.microsoft.comOrderby, ascending, descending. The orderby keyword is not a method per se. It compiles into a method call. It is query expression syntax in LINQ. See the linked article for more information.
OrderBy ClauseLINQ Methods

Different Sort methods

Here we see the three ascending sort methods in the first part of this document together. Each of these three methods returns a copy of the array in sorted form. They all copy first, then sort. The program can be run directly in a new Visual Studio console application.
Program that shows different sort methods [C#]

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
 string[] a = new string[]
 {
     "Salvadorian",
     "Kenyan",
     "Rwandan",
     "Icelandic",
     "African"
 };
 string[] b = SortArray(a);            // b is sorted
 string[] c = SortLinq(a);             // c is sorted

 List<string> l = new List<string>(a);
 List<string> d = SortList(l);         // d is sorted
    }

    static string[] SortArray(string[] a)
    {
 string[] res = (string[])a.Clone();
 Array.Sort(res);
 return res;
    }

    static string[] SortLinq(string[] a)
    {
 var res = from item in a
    orderby item ascending
    select item;
 return res.ToArray();
    }

    static List<string> SortList(List<string> l)
    {
 List<string> res = new List<string>(l);
 res.Sort();
 return res;
    }
}
First method. The first sorting method, named SortArray, demonstrates the use of Clone to make a shallow copy of the parameter array. In SortArray, the string[] method variable is pushed onto the stack and it is then sorted with Array.Sort.
Second method. In the second method, called SortLinq, we use the LINQ query syntax to get an IEnumerable collection of the parameter string[] array that is ordered from A-Z. It then uses ToArray() to evaluate the IEnumerable collection and return. More information is available on the ToArray extension method.
ToArray Extension MethodThird method. The third method, named SortList, receives a List parameter and then calls the List sort method on it. Because List.Sort internally uses Array.Sort, the performance is almost the same. This method copies before sorting.
Sort string debugger screenshot

String array initializer syntax

In the C# language, there are several different syntaxes for instantiating a string[] array. They are normally equivalent in performance, and just vary in appearance. Here we look at the syntax differences in a complete example program.
String Array
String array initializer examples [C#]

class Program
{
    static void Main()
    {
 string[] a = new string[]
 {
     "French",
     "Spanish",
     "Canadian",
     "Mexican",
     "Swedish",
 };
 string[] b =
 {
     "Polish",
     "Israeli",
     "Turkish",
     "Pakistani",
     "Belgian",
 };
 var c = new string[]
 {
     "Iraqi",
     "Iranian",
     "Romanian",
     "Latvian",
     "Norwegian",
 };
    }
}
Note on the three examples. The three local variables above all are string[5] collections, and their syntaxes can all be used interchangeably. Your preferences and your team's guidelines will be important here.

Benchmarks

Here we see that the three helper methods that copy and then sort an array parameter are very close in performance. Next I compare the three methods on arrays of 46, 92, and 460 elements.
Array sorting method [C#]

static string[] SortArray(string[] a)
{
    string[] res = (string[])a.Clone();
    Array.Sort(res);
    return res;
}

LINQ sorting method [C#]

static string[] SortLinq(string[] a)
{
    var res = from item in a
       orderby item ascending
       select item;
    return res.ToArray();
}

List sorting method [C#]

static List<string> SortList(List<string> l)
{
    List<string> res = new List<string>(l);
    res.Sort();
    return res;
}

Benchmark results, 46 strings

Array method: 400 ms
LINQ method:  410 ms
List method:  406 ms

Benchmark results, 92 strings

Array method: 2220 ms
LINQ method:  2290 ms
List method:  2205 ms

Benchmark results, 460 strings

Array method: 14504 ms
LINQ method:  15100 ms
List method:  14350 ms
Interpretation of the results. What I found is that the three methods shown are very similar in performance. In your program, you will need to benchmark them when fine-tuning your own algorithm. We cannot make many generalizations from this experiment.
.NET Framework illustration

Internal sorting routines

Internally, you can see that the List sort method is implemented with Array.Sort. This is why is performs very closely to how the Array.Sort method does. To examine the internals of the List sorting method, you can open IL Disassembler and click on the Sort call.
IL Disassembler Tutorial
Part of implementation of List.Sort [C#]

Array.Sort<T>(this._items, index, count, comparer);
Type specifier. The Array.Sort method shown is a generic method, which means you must specify the type when you call the method. Note that the C# compiler can derive the type implicitly in many cases. This means that Array.Sort is the same as Array.Sort<T>.

Summary

We saw some examples of how to sort strings in the C# programming language, along with a simple benchmark that includes copying of the collections. Be careful to copy your collection first if you don't want to replace it with the sorted instance. We learned that the List sort method and the Array.Sort method are implemented with the same algorithm.

1 comments:

lingmaaki said...

More about ...C# Array

Ling

Post a Comment