codrgii
codrgii

Reputation: 209

list is not sorting at all

i am updating a list and then trying to sort the list putting the highest sVar1 at the front of the list, but it is not doing it, i'm using 6.0 VS

while(Iter != m_SomeList.end())
{
    if((*Iter)->sVar1 == 1) 
    {
        (*Iter)->sVar1++;
    }

    Iter++;
}

m_SomeList.sort(Descending());

Iter = m_SomeList.begin();

while(Iter != m_SomeList.end())
    {
        //now display the content of the list

my descending sort function

struct Descending : public greater<_LIST_DETAIL*> 
{
    bool operator()(const _LIST_DETAIL* left, const _LIST_DETAIL* right)
    {
        return (left->sVar1 > right->sVar1);
    }
};

can anyone spot whats wrong?

edit: updated code, it contained typos...

Upvotes: 2

Views: 116

Answers (1)

John Dibling
John Dibling

Reputation: 101456

This is just all kinds of messed up.

First, you do not derive from std::greater -- you either use std::greater directly, as in:

(psudocode, uncompiled)

std::sort( v.begin(), v.end(), std::greater() );

...or, if that's no good for you, write your own functor by deriving from std::unary_function:

struct Descending : public unary_function<bool, _LIST_DETAIL>
{
    bool operator()(const _LIST_DETAIL* left, const _LIST_DETAIL* right) const // note const
    {
        return (left->sVar1 > right->sVar1);
    }
};

Second, the use of leading underscores followed by an upper case name is reserved by the language.

Third, you are using a 14-year old compiler that MicroSoft has terminated all support for years ago. What's more, by today's standards it is a stinking pile of junk. It wasn't even very good when it was new. You need to get off of VS 6.0.

Upvotes: 2

Related Questions