#include #include #include // comparison, not case sensitive. bool compare_nocase (const std::string &first, const std::string &second) { unsigned int i = 0; while ( (i < first.length()) && (i < second.length()) ) { if (::tolower(first[i]) < ::tolower(second[i])) return true; else if (::tolower(first[i]) > ::tolower(second[i])) return false; ++ i; } if (first.length() < second.length()) return true; else return false; } int main() { std::list mylist; mylist.push_back ("one"); mylist.push_back ("two"); mylist.push_back ("Three"); // case sensitive (default) mylist.sort(); std::list :: iterator i = mylist.begin(); std::list :: iterator e = mylist.end(); for (i; i != e; ++ i) { std::cout << " " << *i; } std::cout << std::endl; // Using the function, compare_nocase(), the comparison is made case insensitive. mylist.sort(compare_nocase); i = mylist.begin(); e = mylist.end(); for (i; i != e; ++ i) { std::cout << " " << *i; } return 0; // sort1.cpp } // Output: // Three one two // one Three two