Sort a string vector ignoring case

This is a discussion on Sort a string vector ignoring case within the C++ Programming forums, part of the General Programming Boards category; How can I sort a string vector ignoring case? Like, if the input is B, c, D the result is ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Sort a string vector ignoring case

    How can I sort a string vector ignoring case? Like, if the input is B, c, D the result is the same and not B, D, c, which is what I get with sort(v.begin(), v.end())

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You'll have to use another form of sort() where it calls your routine, and your routine can do the case insensitive compare.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You need to pass a predicate function to sort() to ignore the case.
    Here's an example from "Exceptional STL" by Scott Meyers:
    Code:
    bool ciCharLess( char c1, char c2 )
    {
        return (std::tolower( static_cast<unsigned char>( c1 ) ) < std::tolower( static_cast<unsigned char>( c1 ) ));
    }
    
    bool CompareNoCase( const std::string& s1, const std::string& s2 )
    {
        return std::lexicographical_compare( s1.begin(), s1.end(), s2.begin(), s2.end(), ciCharLess );
    }
    Last edited by cpjust; 01-31-2008 at 01:33 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Call the 3-argument version of sort() using a sort criterion that does what you want.

    http://www.cplusplus.com/reference/algorithm/sort.html

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Can you elaborate? I'm new to C++, I don't know how sort works.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Quote Originally Posted by jw232 View Post
    Can you elaborate? I'm new to C++, I don't know how sort works.
    Okay.

    There are two types of sort:

    #include <algorithm>
    void sort( iterator start, iterator end );
    void sort( iterator start, iterator end, StrictWeakOrdering cmp );

    In a sentence, an iterator is a component of your data that can move through the range of data, and return the actual data when asked for it. In order for the sort to work, it needs to know where the range of your data begins and ends.

    In the first version, sort will compare elements to eachother using the < operator, and sort the data based on that result. However in the second version, you have the option of passing along some sort of predicate that will do the same type of comparison with your data. Sometimes the predicate is just another function, and at other times it's a structure with a parens operator defined, because the programmer needs to keep track of some state, or the predicate only needs to be alive during the sort because it's only purpose is to be used by the sort, etc.
    Last edited by whiteflags; 01-31-2008 at 01:51 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by jw232 View Post
    Can you elaborate? I'm new to C++, I don't know how sort works.
    You don't need to know how it works, only what it does (It sorts things) and how to use it (I believe a suitable link has been provided).
    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. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21