Thread: sorting ABC order

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    sorting ABC order

    currently I have this method of sorting with c++ (ABC order)
    Code:
    #include <algorithm>
    #include <string>
    #include <ctype.h>
    struct sort_pred {
    
        bool operator()(const string &left,const string &right) {
    
            for(string::const_iterator lit=left.begin(), rit=right.begin(); lit != left.end() && rit != right.end(); ++lit,++rit)
    
            if( tolower(*lit) <  tolower(*rit))
    
                 return true;
    
            else if( tolower( *lit ) > tolower( *rit ) )
    
                 return false;
    
            if( left.length() < right.length() )
    
                return true;
    
            return false;
    
        }
    
    };
    
    int main()
    {
    //some vector<string> vec
    sort(vec.begin(),vec.end(),sort_pred());
    }
    Is there a more simple way to sort words in abc order?
    I have heard that in Java there is simply the "compareTo" method that returns positive if first word is closer to A than the second.. (along with a compareTo for doubles and other types as well)

    is there a similar method in c++ stl?
    Last edited by rodrigorules; 02-21-2010 at 07:48 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes. Lose the predicate function and then it will work.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    hmm what if I was sorting a vector of structs which contain a string?

    i would still need the predicate function right?

    (thats what it really is, but i didnt want to include the struct since it would overcomplicate my question)

    Code:
    struct player
    {
    string name;
    int id;
    }
    
    int main()
    {
    vector<player> players;
    sort(players.begin(), players.end());
    //INCORRECT
    }
    THANKS!!!
    Last edited by rodrigorules; 02-21-2010 at 07:52 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > hmm what if I was sorting a vector of structs which contain a string?
    Then you write a comparison function using the string operators > or <.

    > i would still need the predicate function right?
    Yes in that case you would.

    > (thats what it really is, but i didnt want to include the struct since it would overcomplicate my question)
    No it wouldn't over complicate anything. In fact it led to a completely different answer.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The fact that you're using tolower at all suggests to me that you're looking for a case-insensitive sort of those strings. If this is the case all you do is:
    Code:
    #include <cctype> // for toupper
    #include <string>
    #include <algorithm>
    using namespace std;
    
    bool operator()(string left, string right) {
        transform(left.begin(), left.end(), left.begin(), (int(*)(int)) toupper);
        transform(right.begin(), right.end(), right.begin(), (int(*)(int)) toupper);
        return left < right;
    }
    Now if this is a struct then you wont want to modify the contents of those strings when comparing them, so be sure you make a copy first in that case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not sorting numbers in order.
    By rushhour in forum C++ Programming
    Replies: 21
    Last Post: 02-19-2009, 01:40 PM
  2. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM
  3. Sorting exam score in increasing order
    By superman12 in forum C Programming
    Replies: 5
    Last Post: 07-14-2005, 12:34 AM
  4. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  5. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM