469,271 Members | 1,769 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Sort characters in a string

Hi,

Using C# is there a quick, concise method of sorting the characters of a
string into alphabetic order
Jan 6 '06 #1
3 24571
This is kinda hokey but seems to work:

string s = "zygwernmdkwirjdcndneyakdcmb";
char[] c=s.ToCharArray();
Array.Sort(c);
string s2=String.Empty;
foreach (char ch in c)
s2+=ch.ToString() ;
Console.WriteLine(s2);

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Someone" wrote:
Hi,

Using C# is there a quick, concise method of sorting the characters of a
string into alphabetic order

Jan 6 '06 #2
An optimization: to construct a string from a char[], just use the
string constructor.

public static string SortStringChars(string s)
{
char[] c=s.ToCharArray();
Array.Sort(c);
return new String(c);
}

Thi

Jan 6 '06 #3
Hi,

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:54**********************************@microsof t.com...
This is kinda hokey but seems to work:

string s = "zygwernmdkwirjdcndneyakdcmb";
char[] c=s.ToCharArray();
Array.Sort(c);
string s2=String.Empty;
foreach (char ch in c)
s2+=ch.ToString() ;


You are creating a bIG number of strings here, just use the overloaded
constructor of String ( char[] ) or use StringBuilder instead.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by duncan.lovett | last post: by
6 posts views Thread by Al Newton | last post: by
4 posts views Thread by jarkkotv | last post: by
14 posts views Thread by william.hooper | last post: by
8 posts views Thread by pabl0 | last post: by
2 posts views Thread by Paul E Collins | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.