TheDeveloperBlog.com


C# Sort Ignore Leading Chars

Sort ignore leading chars. Strings sometimes contain characters that do not matter. These characters can be ignored while sorting. For example we can ignore the period on the start of ".NET". The string will be sorted by the N not the period.


Example. To begin, this program uses a four-element string array with some values that have leading punctuation. The value "(Z)" is by default sorted by its parenthesis character, not the Z. The value ".NET" is sorted by the period.

String Array

And: The result is that the order is unnatural and hard to scan. The period should be ignored.

Program with sort that ignores characters: C#

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	string[] elements = { "A", "(Z)", ".NET", "NO" };
	{
	    var sorted = from element in elements
			 orderby element
			 select element;

	    foreach (var element in sorted)
	    {
		Console.WriteLine(element);
	    }
	}
	Console.WriteLine("---");
	{
	    var sorted = from element in elements
			 orderby element.TrimStart('(', '.')
			 select element;
	    foreach (var element in sorted)
	    {
		Console.WriteLine(element);
	    }
	}
    }
}

Output

(Z)
.NET
A
NO
---
A
.NET
NO
(Z)

In the second query expression, we implement the code that ignores the leading parenthesis and period characters before considering the strings. To do this, we call TrimStart on the identifier in the orderby part of the clause.

Note: The sort key does not include leading punctuation, which leads to a more natural sorted order.

TrimEnd, TrimStartOrderBy Clause

Discussion. The code here is not optimally fast. But it is pretty clear and easy to understand. If you want to optimize the performance of this method, I would consider implementing IComparer and using Array.Sort and sorting the array in-place.

Array.Sort Examples

Summary. You can implement custom sorts using the query expression syntax in the C# language. This gives you the ability to sort on slightly mutated strings, such as ones that are stripped of their leading characters.

And: This can lead to more natural, usable sorted arrays, and better interface usability.