Solved

stl list sorting

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 column That will then direct you to their download page. From that p…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

724 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