Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-02-2005, 11:46 PM   #1
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
C++ Sort()

I'm using the sort function in my c++ program. This works great, but it sorts case-sensitive.
Here's what I'm using now:
sort(files.begin(), files.end());
Is there a way to sort case-insensitive ?

Thanks in advance,
Old 11-03-2005, 12:23 AM   #2
Registered: Nov 2004
Location: china
Distribution: Mandrake 10.1 Official
Posts: 37

Rep: Reputation: 15
you can convert all text chars to UP or to LOW and then sort(),now if is it case-insensitive?
Old 11-03-2005, 12:26 AM   #3
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
I figured it out.
It uses the same idea you mentioned anuode

For anyone else who wants to do this see:
I couldn't get it to compile using this directly, I had to change the 'caseInsensitiveComp' function so it looked like this:
bool caseInsensitiveComp(string left, string right) {
  // Convert the strings to all lowercase.
  transform(left.begin(), left.end(), left.begin(), (int(*)(int))tolower);
  transform(right.begin(), right.end(), right.begin(), (int(*)(int))tolower);
  // Compare the strings.
  return left < right;          // string class has <
make sure you include <cctype> and <algorithm>

Hope this helps anyone else who has this problem.

Last edited by bendeco13; 11-03-2005 at 12:28 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sort the output of ls bahadur Programming 18 03-28-2005 08:08 PM
Hello (sort of...) Gethyn Member Intro 1 10-15-2004 01:10 PM
sort pantera Programming 5 05-26-2004 08:36 PM
D[a]mned sort J_Szucs Linux - Software 3 09-21-2003 04:42 PM
Bubble sort? Becks Programming 4 09-12-2003 02:28 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration