Hi All,
I will be highly obliged if you could solve my issue.
The issue which I am facing that is about the sorting order of the Special Chars, Numbers and Alphbets.
I think default sorting is Special Chars -> Numbers -> Alphbets in Ascending order. Suppose I have below data
"Test1","12234","23","@abc","@123"
if we sort the above data in c# in ascending order then we will get the following result
"@123","@abc","12234","23","test1"
But as per my requirement , I want the sorting to be Numbers -> Special Chars ->Alphabets
The above result should come as
"12234","23","@123","@abc","test1"
And these results I am displaying in the DataGrid, So I want to change the way of default sorting of the grid so that the above result can be achieved.
Loading

Jignesh TrivediPosted Apr 10, 2014, 1:18 AM
Hi,
Agree with Vulpes, using custom comparator we can do sorting of array as we want.
Please refer
http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/SortarraylistusingcustomIComparer.htm
hope this will help you.
VulpesPosted Apr 9, 2014, 8:04 PM
So I've altered it now so that it truly is case insensitive :)
VulpesPosted Apr 8, 2014, 4:55 PM
I've written one below which seems to be working OK but I'd check it carefully to ensure I've got the ASCII characters in the range 32 - 126 in the correct order.
I've ignored case when sorting letters so A or a will sort before B or b etc.
Control characters (< 32) or non-ASCII characters (> 126) should sort normally.
using System;
using System.Collections.Generic;
public class NSAComparer: IComparer
{
private const string nsa = @"0123456789A !""#$%&'()*+,-./:;<=>?@[\]^_`{|}~abcdefghijklmnopqrstuvwxyz";
public int Compare(string s, string t)
{
s = s.ToLower();
t = t.ToLower();
int min = Math.Min(s.Length, t.Length);
for(int i = 0; i < min; i++)
{
if (s[i] < 32 || t[i] < 32 || s[i] > 126 || t[i] > 126)
{
if (s[i] < t[i])
return -1;
else if (s[i] > t[i])
return 1;
}
else if (nsa.IndexOf(s[i]) < nsa.IndexOf(t[i]))
return -1;
else if (nsa.IndexOf(s[i]) > nsa.IndexOf(t[i]))
return 1;
}
if (s.Length < t.Length)
return -1;
else if (s.Length > t.Length)
return 1;
else
return 0;
}
}
class Program
{
static void Main()
{
string[] words = {"Test1","12234","23","@abc","@123"};
Array.Sort(words, new NSAComparer());
foreach(string word in words) Console.WriteLine(word);
Console.ReadKey();
}
}
The output is:
12234
23
@123
@abc
Test1
If you're happy with this, perhaps I could leave you to change the default sorting order to it in whatever DataGrid you're using.