Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C Sharp only
Search
Advanced Search
Forums
BYTES
Product Launch
Updates
Developer Toolkit
Member List
Calendar
Today's Posts
Home
Forum
Topic
C Sharp
Sort characters in a string
Sort characters in a string
Collapse
This topic is closed.
X
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
Someone
#1
Sort characters in a string
Jan 6 '06, 01:05 AM
Hi,
Using C# is there a quick, concise method of sorting the characters of a
string into alphabetic order
Peter Bromberg [C# MVP]
#2
Jan 6 '06, 01:55 AM
RE: Sort characters in a string
This is kinda hokey but seems to work:
string s = "zygwernmdkwirj dcndneyakdcmb";
char[] c=s.ToCharArray ();
Array.Sort(c);
string s2=String.Empty ;
foreach (char ch in c)
s2+=ch.ToString () ;
Console.WriteLi ne(s2);
--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
Peter Bromberg's UnBlog
http://petesbloggerama.blogspot.com
Blog, Peter Bromberg, programming, unblog
"Someone" wrote:
[color=blue]
> Hi,
>
> Using C# is there a quick, concise method of sorting the characters of a
> string into alphabetic order
>[/color]
Comment
Post
Cancel
Truong Hong Thi
#3
Jan 6 '06, 05:25 AM
Re: Sort characters in a string
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
Comment
Post
Cancel
Ignacio Machin \( .NET/ C# MVP \)
#4
Jan 6 '06, 02:35 PM
Re: Sort characters in a string
Hi,
"Peter Bromberg [C# MVP]" <pbromberg@yaho o.nospammin.com > wrote in message
news:54980D64-67C4-4516-A866-EC2007723E7B@mi crosoft.com...[color=blue]
> This is kinda hokey but seems to work:
>
> string s = "zygwernmdkwirj dcndneyakdcmb";
> char[] c=s.ToCharArray ();
> Array.Sort(c);
> string s2=String.Empty ;
> foreach (char ch in c)
> s2+=ch.ToString () ;[/color]
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
Comment
Post
Cancel
Previous
template
Next
Working...
OK
OK
Cancel
👍
👎
☕
Comment