# Sorting Strings

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 08-20-2003
Derek5272
Sorting Strings
I'm about to start work on a program that will sort strings in alphabetical/numerical order. I searched the tutorials and the FAQ, but I didn't see anything that would help with this. Could anybody give me some sample code to get me started on this? Thank you in advance.
• 08-20-2003
major_small
just check the first element in the array and move then around that way...
• 08-20-2003
Derek5272
Um... That's exactly what I don't know how to do. Can you post a snippet of code to get me started?
• 08-20-2003
XSquared
Code:

```for( int i = 0; i < numStrings; i++ ) {         for( int j = i; j < numStrings - 1; j++ )         {                 if( strings[ j ] <  strings[ j + 1 ] )                 {                         string temp = strings[ j ];                         strings[ j ] = strings[ j + 1 ];                         strings[ j + 1 ] = temp;                 }         } }```
• 08-20-2003
Derek5272
Thank you! Now I've gotta copy it onto my laptop... Ah well. Thanks again.

Edit: Sorry, maybe I'm just stupid. I can't figure out how that works. Could you possibly tell me how?
• 08-21-2003
Derek5272
Nobody can help me out anymore? Guess I have to wait another 5 hours.
• 08-21-2003
ripper079
Quote:

Nobody can help me out anymore? Guess I have to wait another 5 hours.
Well havn´t XSquared answered your question? XSquared uses a sort algorithm called bubble sort. Make a serach here or on The beloved google insteed of waiting.
• 08-21-2003
joshdick
I suggest a different solution. After all, this is the C++ forum, and what's the point of programming in C++ if you're not going to write some classes?

Write a string class. Overload the boolean operators for this class. Once you implement that, you can use any ole sort you please that makes use of the boolean operators. I love classes :)
• 08-21-2003
golfinguy4
Include the algorithm, string, and vector headers... then, put all the strings in the vector and use the following:

Code:

```#include <algorithm> #include <string> #include <vector> int main(void) {    std::vector< std::string > myList;       //populate myList with data       std::sort(myList.begin(), myList.end());       //myList is now sorted }```

If you want to use your own method of comparison instead of the default operator<, give the sort function a third parameter of your sorting function. For more info, check out here
• 08-22-2003
Derek5272
Ok, I'm starting to get really confused...

Anyway, yesterday I found code for this in the source code section of this site (here). I copied that out, modified it to C++ commands and stuff, and it generated an error on the line:

Code:

`SWAP(a[j-1],a[j]);`
My compiler tells me that there is a syntax error on that line, and that a ';' is missing before the '}'. Now, I can see that there's no right brace on that line, so is my compiler just messed up (Microsoft Visual C++ 6.0 Intro. Ed.)? Or is there something I'm missing?
• 08-22-2003
yakabod
ive been seeing std in codes and stuff. and i know that it deals with strings but what else does it do and deal with? i dont really know what it is as for me getting confused and being a noob. seeing it in alot of peoples layout and dont know what they mean..
• 08-22-2003
JaWiB
If your speaking of std like in this code:
Code:

```#include <string> int main(void) { std::string myStr("string"); }```
it means you are creating a string object from the std (standard) namespace...After learning C++ basics I would suggest "The C++ Standard Library" by Nicolai M. Josuttis. I recently bought it and it has been great for a reference on the standard library, although I've been told its more of a standard template library reference.
• 08-22-2003
yakabod
Quote:

I would suggest "The C++ Standard Library" by Nicolai M. Josuttis.
alright thanks let me write that in my notebook :) so this should tell me about the standard librarys right? and is it easy to understand?
• 08-22-2003
JaWiB
Its not really that easy to understand so like I said make sure you have a good grasp on C++ before trying this book

Although many people would say its not a good book, I found "Sam's Teach Youself C++ in 21 Days" to be easy to understand and it included a variety of subjects

Edit: And as long as you get the most recent version it was also compliant with C++ standard AFAIK (uses the std namspace, etc.)
• 08-22-2003
Derek5272
Ok... Now, could we get back on topic and answer my question? Thanks :)
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last