How do you sort a list of strings ???

Started by
12 comments, last by iMalc 18 years, 8 months ago
I have a list of strings that I want to sort:

list <string> lstStrings;

lstStrings.push_back("BBBBBB");
lstStrings.push_back("AAAAAA");
lstStrings.push_back("CCCCCC");

lstStrings.sort();

How do I sort the list of strings ascending and descending?

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Advertisement
The list's sort() member function can take a sorting criterion as a parameter. i.e. the function to use to determine whether A < B.

Example:

#include <list>#include <functional>using namespace std;list<string> lstStrings;lstStrings.push_back("BBBBBB");lstStrings.push_back("AAAAAA");lstStrings.push_back("CCCCCC");// descending orderlstStrings.sort( greater<string>() );


Note that VC6's implementation is flawed and will not accept this code. I can't remember the workaround right now, sorry.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
whats ascending?
IS it:
less<string>()

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

A link that might be useful to you is this link.

A lot of algorithms are already provided in STL and a lot of people seem not to use them, while they should :)

Hope this helps

Eric
Quote:Original post by utilae
whats ascending?
IS it:
less<string>()


Yes, that's the default comparison criterion. For a given comparison criterion f and two objects A and B, then A comes before B in the sorted output if f(A,B) is true. It's easy to see that less leads to an ascending order (assuming that operator<() has been implemented properly for the type being considered).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
By the way, when sorting you can also give your own predicate, something like this :

// Tests for something.. like the lenght of the string
bool MyTestPredicate(const std::string& in_lhs, const std::string& in_rhs)
{
return in_lhs.size() < in_rhs.size();
}

std::sort(mylist.begin, mylist.end, MyTestPredicate);

(I haven't compiled anything, but I guess this should work...)

This way, if you're storing structs that you'd like to be sorted in different ways, like based on different members or something, you can have a few predicates and select the one appropriate to your operation.

Hope this helps

Eric
Quote:Original post by xEricx
std::sort(mylist.begin, mylist.end, MyTestPredicate);

(I haven't compiled anything, but I guess this should work...)


You should really avoid using std::sort with a std::list. Even if it works (which isn't guaranteed), the lack of random access will kill performance. That's why std::list has its own sort() member function.

With that caveat, I agree with your observation. It's just that the standard library already does provide the basic functors.

... and if you were using boost::lambda, you could even write mylist.sort(_1<_2); for ascending order and mylist.sort(_2<_1); for descending order.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok thanks guys I got it now.

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Quote:Original post by Fruny
Quote:Original post by xEricx
std::sort(mylist.begin, mylist.end, MyTestPredicate);

(I haven't compiled anything, but I guess this should work...)


You should really avoid using std::sort with a std::list. Even if it works (which isn't guaranteed), the lack of random access will kill performance. That's why std::list has its own sort() member function.


Couldn't/why don't they do tag dispatching in std::sort to fix this? Right now if you have a typedef for some container type, and you switch to/from a list, you may need to fix sort calls as well :s I can understand not providing an operator[], but list sorting (via mergesort or whatever) is within the same order of complexity as general-case sorting with random access being available :/
To sort strings ascending, sort the last character first, and move towards the first.

This topic is closed to new replies.

Advertisement