Solved

stl list sorting

Posted on 2004-04-29
5
14,187 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
  • 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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
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 columnThat will then direct you to their download page.From that page s…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

776 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