Thread: Sorting Strings

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    43

    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.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just check the first element in the array and move then around that way...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    43
    Um... That's exactly what I don't know how to do. Can you post a snippet of code to get me started?
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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;
    
    		}
    
    	}
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    43
    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?
    Last edited by Derek5272; 08-21-2003 at 09:10 AM.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    43
    Nobody can help me out anymore? Guess I have to wait another 5 hours.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    43
    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?
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    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..

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    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?

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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.)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok... Now, could we get back on topic and answer my question? Thanks
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM