# String sort in Alphabetical order

• March 22nd, 2010, 12:10 PM
Neil.Dyke
String sort in Alphabetical order
Hello,

Does anyone have an algorithm or know a function that can sort a std::string for me?

Say i have:

string one = "test";

sorting it alfabetically would result in > "estt"

Thanks,

Neil.
• March 22nd, 2010, 12:11 PM
MrViggy
Re: String sort in Alphabetical order
Any numerical sort function should work. Each character is just an integer.

Viggy
• March 22nd, 2010, 12:12 PM
Neil.Dyke
Re: String sort in Alphabetical order
Do you have some code? I'm lazy ;)
• March 22nd, 2010, 12:16 PM
MrViggy
Re: String sort in Alphabetical order

Viggy
• March 22nd, 2010, 12:23 PM
TheRogue
Re: String sort in Alphabetical order
Check out std::sort in <algorithm>, it was just a google away :)

http://www.cplusplus.com/reference/algorithm/sort/
• March 22nd, 2010, 12:27 PM
kempofighter
Re: String sort in Alphabetical order
If you are one of those people that refuses to use the std algorithms for whatever reason there are many examples of sorting functions on the internet that you can find, copy, and paste. there is no reason for anyone else on this forum to post code when you can find examples in about 30 seconds.
• March 22nd, 2010, 02:00 PM
Lindley
Re: String sort in Alphabetical order
What result do you want for the string "cbaCBA"? In other words, are you looking for a case-invariant sort?
• March 22nd, 2010, 02:39 PM
Neil.Dyke
Re: String sort in Alphabetical order
Its only caps.

so for example i need to sort the string "TEST" to "ESTT".
• March 22nd, 2010, 04:27 PM
Lindley
Re: String sort in Alphabetical order
Great, then you don't even need to write a custom comparator for std::sort.