Sorting strings

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
 
N

not_a_commie

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top