Jump to content
  • Advertisement
Sign in to follow this  
utilae

How do you sort a list of strings ???

This topic is 4771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 order
lstStrings.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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!