Thread: Sorting STRING arrays

  1. #1

    Angry Sorting STRING arrays

    Can somebody help me before i shoot myself? I'm trying to sort arrays.. i did it perfectly with numbers.. then i go to sort a list of names alphabetically and BOOyah! the same routine doesn't work! it works in Qbasic! :*( Somebody told me to try the "strcmp" function. I did.. nothing really came of that.. if anyone can help me, then that would be appreciated!


  2. #2
    Registered User
    Join Date
    Jun 2002
    I'm curious... can I see the algorithm you used? Maybe then I can help you 'tweak' it to work correctly with string objects

  3. #3
    Registered User
    Join Date
    Feb 2002
    the problem could be that comparing two strings isn't the same as comparing two numbers. the string would be a char * or a char[] and the number would be of type int. When examining the bits and pieces of this, you'll notice that the char*/char[] is acctually a 32 bit number pointing to some memory location. a compariosson of the variables String1 == String2 would acctually compare two memory locations, not two strings, since they are really two numbers and not two strings. Number1 == Number2 on the other hand will compare the numbers. To compare the strings you must compare whats on those memory locations instead... strcmp does that for you, when properly used...

    What might ease your pain is the use of the CString or the std::string data type/class. these classes have the == operator overloaded to do an compare of the strings instead of the mem locations...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Why don't you just use the STL set container along with the string class?

    #include <iostream>
    #include <set>
    #include <string>
    using namespace std;
    set<string> StringSet;
    set<string>::iterator it;
    StringSet.insert( string("Sarah") );
    StringSet.insert( string("Harold") );
    StringSet.insert( string("Mabel") );
    StringSet.insert( string("John") );
    StringSet.insert( string("Abel") );
    StringSet.insert( string("Donny") );
    for( it = StringSet.begin(); it != StringSet.end(); ++it )
        cout << *it << endl;
    Sets automatically sort things that you add to them. If I've done everything properly, this should print out the following:
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    [ code ]
    code here
    [ /code ]

  6. #6
    strcmp() compares two strings char by char. The decision of which string is "larger" than the other depends on the result of the first instance of difference between char with the same index in the two words, if there is one. strcmp() is sensitive to both case and size (unlike stricmp() and strncmp()). These issues can be resolved with cosideration of the character set as follows: null is zero and upper case comes before lower case and therefore has lower ASCII values. Thus A is less than a and cat is less than catapult.

    The prototype is:
    int strcmp(const char * first, const char * second);

    and the results are as follows:
    if first > second, return value > 0;
    if first == second, return value == 0;
    if first < second, return value < 0;

    therefore to sort C style strings in ascending order using strcmp():

    if(strcmp(first, second) > 0)
    //switch first and second

    embed this logic in a couple of loops implementing your favorite sorting algorhythm and away you go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM