473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 25187
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: duncan.lovett | last post by:
I am working on a graphical heading generator for a clients website as their server does not have the GD library or similar plugins for dynamic image generation. I have achieved the result...
6
by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
4
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
14
by: william.hooper | last post by:
There is a longer article about this subject here: http://www.codeproject.com/useritems/SortedList_Bug.asp See the main article and the reply thread started by Robert Rohde. Alternatively look...
2
by: gonzo | last post by:
So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the...
8
by: pabl0 | last post by:
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted. an example is in order. ...
15
by: Academia | last post by:
I want a string of characters that will sort after all strings of Roman letters. Something that will display using a font like the one this note is written with. Is there such a thing? I...
2
by: Paul E Collins | last post by:
char c = { 'a', 'b', 'c', 'A', 'B', 'C' }; Array.Sort(c, CaseInsensitiveComparer.Default); After this, c is ordered { 'A', 'B', 'C', 'a', 'b', 'c' }. I want letters to be sorted together...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.