Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to sort std list?

Started by utilae Feb 25, 2005 at 4:46 PM 11 replies 1.7k views
Original Post
utilae
utilae
Hi, I am using C++ and Direct3d9. I have a list of RECTS,eg list lstRectList; I want to sort either ascending or descending by a value, eg sort by RECT.left So how would I do it?
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
Evil Steve
Evil Steve
Like so:
struct MyCompare{   bool operator() (const RECT& a, const RECT& b)      {return a.left < b.left;}}std::sort(lstRectList.begin(),lstRectList.end(),MyCompare);


EDIT: Or you could make your own RECT structure, and add an operator< to it, then call std::sort(lstRectList.begin(),lstRectList.end());
JimPrice
JimPrice
You should be able to do this with list.sort(op), for a predicate op.

#include <iostream>#include <list>#include <algorithm>bool myPred(int i1, int i2){	return (i1 < i2);}int main(){	std::list<int> myInt;	myInt.push_back(1);	myInt.push_back(2);	myInt.push_back(0);	std::copy(myInt.begin(), myInt.end(), std::ostream_iterator<int>(std::cout, " "));	myInt.sort(myPred);	std::copy(myInt.begin(), myInt.end(), std::ostream_iterator<int>(std::cout, " "));	std::cin.get();	return 0;}


Jim.

Edit : yup, thought I'd been slow...
JohnBolton
JohnBolton
Wow. You have to be fast to be first in these forums!

bool MyListSorter( RECT & a, RECT & b ){    return a.left < b.left;}    ...    lstRectList myList;    myList.sort( MyListSorter );    ... 
or
class MyListSorter{public:    bool operator()( RECT & a, RECT & b )    {        return a.left < b.left;    }};    ...    lstRectList myList;    myList.sort( MyListSorter() );    ... 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
utilae
utilae
Thanks, I'll try one of these methods.
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
utilae
utilae
I have a struct like this:
struct MyRECT{     int m_nWidth;     int m_nHeight;     int m_nArea;};


And my list is like this:
list <MyRECT*> m_lstRECTS;


How would I sort this list?
I tried using the below method, but I can't figure it out:
//list::sortm_lstRECTS.sort( SortList() );//sortlist function to use with list::sortbool SortList(MyRECT *a,MyRECT *b){    return a->m_nArea < b->m_nArea;}
HTML5, iOS and Android Game Development using Corona SDK and moai SDK
dave
dave
vector is usually better.

ace
Palidine
Palidine
just literally copy/past EvilSteve's response and fillout the comparitor differently if you want. you have several things wrong i believe: your comparison function is defined wrong, it's defined _after_ you use it, and it's passed w/ () to the std::sort function. just copy/paste/edit.

-me
Evil Steve
Evil Steve
Quote:
Original post by ace_lovegrove
vector is usually better.
std::vector is *far* slower for insertion, except for at the end. It's better for some things (random access, O(1) lookup), but worse for others. I wouldn't say that vector is "usually better" by any means.
JimPrice
JimPrice
Quote:

just literally copy/past EvilSteve's response and fillout the comparitor differently if you want.


My comment on that would be that EvilSteve used an algorithm; it is generally preferable to use a member function (where available) over an algorithm; hence the examples by petewood, johnbolton and myself.

Quote:

I tried using the below method, but I can't figure it out:


If the advice of making sure that the function declaration is prior to the use of it in an algorithm doesn't work, make sure you post the error message as well - otherwise it's hard to know what's going on.

Oh yeah - and this:

m_lstRECTS.sort( SortList() );

should be this:

m_lstRECTS.sort( SortList );

Note the removal of some brackets.

Jim.
Evil Steve
Evil Steve
Quote:
Original post by Anonymous Poster
Quote:
Original post by Evil Steve
EDIT: Or you could make your own RECT structure, and add an operatorstd::sort(lstRectList.begin(),lstRectList.end());


std::sort does NOT work on std::list::iterator's because they're not random access.
D'oh, you're right. I didn't know that...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.