Solved

stl list sorting

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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: …
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

756 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