i’m trying to set a list by desc order, setting the highest to the front of the list, but it is not doing it, here is the struct i am using
struct datalist
{
short index;
int nNumber;
bool operator > (const datalist& a) const
{
return (nNumber > a.nNumber);
}
};
and i am doing
datalist* pAR = new datalist;
pAR->index = 1000;
pAR->nNumber = 10;
m_SomeList.push_back(pAR);
pAR = new datalist;
pAR->index = 1005;
pAR->nNumber = 30;
m_SomeList.push_back(pAR);
pAR = new datalist;
pAR->index = 0;
pAR->nNumber = 20;
m_SomeList.push_back(pAR);
m_SomeList.sort(greater<datalist*>());
after the sort, it is outputting 20, 30, 10, i am using VS 6.0, and before i get comments of i need to upgrade, i know that is an old compiler but in my case i need it, so please keep that in mind when answering, the best descriptive, helpful answer gets the vote.
You are sorting a container of pointers, so the container uses
std::greateron the pointers so they are sorted by address in non-descending order.You want to do something like this:
or
Also as chris mentioned, you might want to be storing these objects by value instead of by pointers so you don’t have to clean them up.