TheDeveloperBlog.com

Home | Contact Us

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

C# Sort Ignore Leading Chars

This C# article shows how to sort while ignoring the first chars in strings. It provides example code.

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.


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