Alphabetize your List or array of strings. You usually have a small or medium number of strings. Explore what performance problems exists with the methods. Additionally, you want to see examples of sorting and some samples of how the methods work.
| Input array | Required result |
|
Zimbabwe America India Germany China Ukraine |
America China India Germany Ukraine Zimbabwe |
First, let's review some important framework methods we will be using. Afterwards, I will demonstrate three different methods of sorting strings in C#. Here's an overview of the framework methods.
With the next methods. They copy and return a sorted collection. If it suffices to sort an array or List in-place, you won't need to copy. Here are the Array method, then the LINQ method, and finally the List method.
/// <summary>
/// Clone the array, then sort the clone.
/// </summary>
/// <param name="array">The input array.</param>
/// <returns>The sorted copy.</returns>
public static string[] SortArray(string[] array)
{
string[] res = (string[])array.Clone();
Array.Sort(res);
return res;
}
/// <summary>
/// Use LINQ to get a sorted IEnumerable, then convert it to
/// an array.
/// </summary>
/// <param name="array">The input string array.</param>
/// <returns>The new sorted array.</returns>
public static string[] SortLinq(string[] array)
{
var res = from item in array
orderby item ascending
select item;
return res.ToArray();
}
/// <summary>
/// Copy the List using the copy constructor. Then sort the copy.
/// </summary>
/// <param name="list">The input list.</param>
/// <returns>The sorted copy.</returns>
public static List<string> SortList(List<string> list)
{
List<string> res = new List<string>(list);
res.Sort();
return res;
}
We can sort any kind of string array. For the List method, we will use an equivalent List object. Let me show a static string array declared with the array initializer syntax in C# 3.0 and LINQ. The functions can sort string arrays like the following one.
/// <summary>
/// The class that contains the string objects.
/// </summary>
class StringObjects
{
/// <summary>
/// An array of strings we want to sort or process.
/// </summary>
static string[] _sortArrays = new string[]
{
"Egyptian",
"Indian",
"American",
"Chinese",
"Filipino",
"Indonesian",
"Korean",
"Japanese",
"English",
"German",
"French",
"Italian",
"European",
"Irish",
"Vietnamese",
"Australian",
"Mongolian",
"Russian",
"Austrian",
"Brazilian",
"Salvadorian",
"Kenyan",
"Rwandan",
"Icelandic",
"African",
"French",
"Spanish",
"Canadian",
"Mexican",
"Swedish",
"Polish",
"Israeli",
"Turkish",
"Pakistani",
"Belgian",
"Iraqi",
"Iranian",
"Romanian",
"Latvian",
"Norwegian",
"Ukrainian",
"Chilean",
"Liberian",
"Libyan",
"Nicaraguan",
};
}
Use them as shown in the next block. Remember that these methods copy the arrays you pass into them, so you can preserve the original. It is faster to sort them in-place, but in my experience I have found it more useful to copy the arrays.
/// <summary>
/// This method uses the three StringSort methods. The last part shows
/// a new List being created. StringOjbects._sortArrays is simply a string[]
/// array and you can replace it.
/// </summary>
static void SortTest()
{
string[] sortedArray1 = StringSort.SortArray(StringObjects._sortArrays);
string[] sortedArray2 = StringSort.SortLinq(StringObjects._sortArrays);
List<string> unsortedList1 = new List<string>(StringObjects._sortArrays);
List<string> sortedList1 = StringSort.SortList(unsortedList1);
}
They are all fast. I don't want to mislead you and show you how to use a really inferior method to sort your string array. I created a benchmark that measures the performance of the three methods I showed. The three methods I show will sort these 3 arrays.
| Array ID | Bars | Number of string elements |
| 1 | First 3 bars on left | 92 |
| 2 | Middle 3 bars | 460 |
| 3 | Last 3 bars on right | 46 |
Result summary. The graph benchmarks the 3 methods each on 3 data sets. Look at the relative heights of the first three, second three, and final three vertical bars. Array.Sort doesn't really have any performance advantage, and that LINQ performs better or equal to any other method I tested.
Use these sorting methods, which are also available as source code. The List sort method is very fast and probably even better than the Array method. The methods in this document do not sort alphanumerically. They do an ASCII sort.