Saturday, 28 May 2016

.Net sorting list with special characters

Sorting using c# .Net  doesn't use ASCII while sorting, It uses Unicode. When we perform a string sort, .Net by default uses the current culture.

Sorting a list in c# with special characters like "-", "/"

Example:
1.  Here is the Sample Data before sorting.


























We can't get sort String having the special characters, Now we can do with RegExpression.

using System.Text.RegularExpressions;


Ascending

response.Result = response.Result.OrderBy(f => Regex.Replace(f.Name.TrimStart().TrimEnd(), "[^a-zA-Z0-9_]+", " ")).ToList();


Descending

   response.Result = response.Result.OrderByDescending(f => Regex.Replace(f.Name.TrimStart().TrimEnd(), "[^a-zA-Z0-9_]+", " ")).ToList();


Result:

























































No comments:

Post a Comment