Solved

stl list sorting

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now