LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-02-2005, 10:46 PM   #1
bendeco13
Member
 
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:
Code:
sort(files.begin(), files.end());
Is there a way to sort case-insensitive ?

Thanks in advance,
Bendeco
 
Old 11-02-2005, 11:23 PM   #2
anuode
Member
 
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-02-2005, 11:26 PM   #3
bendeco13
Member
 
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:
http://www.cs.trinity.edu/~joldham0/...rint-sorted.cc
I couldn't get it to compile using this directly, I had to change the 'caseInsensitiveComp' function so it looked like this:
Code:
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.
Thanks,
Bendeco

Last edited by bendeco13; 11-02-2005 at 11:28 PM.
 
  


Reply



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 07:08 PM
Hello (sort of...) Gethyn LinuxQuestions.org Member Intro 1 10-15-2004 12:10 PM
sort pantera Programming 5 05-26-2004 07:36 PM
D[a]mned sort J_Szucs Linux - Software 3 09-21-2003 03:42 PM
Bubble sort? Becks Programming 4 09-12-2003 01:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 AM.

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