?
Solved

stl list sorting

Posted on 2004-04-29
5
Medium Priority
?
14,194 Views
Last Modified: 2013-11-17
Hello experts,

I have a stl list, and I want to sort it case insensative...

How do I do this?

My code looks like this:

//-------------------------------------------------------
list<String> *li = new list<String>();

for(int i(0); i<8; i++){
  li->push_back(colHeaders[i]);
}

li->sort();

//-------------------------------------------------------

This sorts the list, but All Strings in uppercase are put in front.
I want a case-insensative sort.

thx in advance
0
Comment
Question by:secuteamers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10950764
would it be an option to make the strings all lower- or all uppercase?
0
 
LVL 13

Accepted Solution

by:
SteH earned 2000 total points
ID: 10950855
But you can call sort with an argument. That is a functor for comparing two elements. That way you can define yourself how the comparison is done. You just have to code that function.

An example is at
http://www.sgi.com/tech/stl/functors.html
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 10951168
Thats because i.e. "A" < "a" in ascii hex representation.
What you have to do is to make a buffer list to sort with all uppercase or lowercase assign an int to every item and after you sort the buffer list according to the int you assigned reconfigure your original list.

This way looks more logical. If it is an easier way I'm not familiar with...:-)

gtokas.
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10954306
Probably not the most efficient way in the world to do it but you can use this for case insensitive comparison of strings in a list

#include <string>
#include <list>
#include <iostream>

#include <stddef.h>

using namespace std;
typedef list< std::string > STRING_LIST;

bool caseInsensitiveSort(const string& str1, const string& str2)
{
  string tmpStr1 = str1;
  string tmpStr2 = str2;

  //convert arguements to lower case
  for(unsigned i = 0; i < str1.length(); i++)
    tmpStr1[i] = tolower(tmpStr1[i]);
  for(unsigned i = 0; i < tmpStr2.length(); i++)
    tmpStr2[i] = tolower(tmpStr2[i]);

  return ( strcmp(tmpStr1.c_str(), tmpStr2.c_str()) < 0);
}

int main(int argc, char* argv[])
{
  STRING_LIST stringList;

  stringList.push_back("Zebra");
  stringList.push_back("Hello");
  stringList.push_back("HELLO");
  stringList.push_back("Bob");
  stringList.push_back("zebra");

  stringList.sort(caseInsensitiveSort);

  for(STRING_LIST::iterator i = stringList.begin(); i != stringList.end(); i++)
  {
    cout << i->c_str() << endl;
  }

  return 0;
}
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10954321
so I guess your code would look like this

bool caseInsensitiveSort(const string& str1, const string& str2)
{
  string tmpStr1 = str1;
  string tmpStr2 = str2;

  //convert arguements to lower case
  for(unsigned i = 0; i < str1.length(); i++)
    tmpStr1[i] = tolower(tmpStr1[i]);
  for(unsigned i = 0; i < tmpStr2.length(); i++)
    tmpStr2[i] = tolower(tmpStr2[i]);

  return ( strcmp(tmpStr1.c_str(), tmpStr2.c_str()) < 0);
}


list<String> *li = new list<String>();

for(int i(0); i<8; i++){
  li->push_back(colHeaders[i]);
}

li->sort(caseInsensitiveSort);
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question