Sorting strings

  • Thread starter Thread starter David Jackson
  • Start date Start date
D

David Jackson

Hello,

I have the following code for sorting strings alphabetically:

string strRaw = "Thisisastring";
char[] astrSorted = strRaw.ToCharArray().ToArray();
Array.Sort(astrSorted);
string strSorted = String.Empty;
foreach (char strChar in astrSorted)
{
strSorted += strChar;
}

Apart from the fact that it's probably not very efficient, the problem I
have is that it sorts according to the ASCII value of each character, which
means that all the upper-case letters appear before all the lower-case
letters.

Can anyone please help me with a method to sort strings so that they would
appear as e.g. "AaBbCcDdEe" etc.

Thank you.

DJ
 
You need to incorporate the string.Compare function. To do that,
though, you'll need to compare strings, not chars. Pass that function
into your sort call.
 
Back
Top