sorting std::list with function predicate

From: matthias_k (nospam_at_digitalraid.com)
Date: 01/23/05


Date: Sun, 23 Jan 2005 11:50:21 +0100

Hi,

I need to sort elements of a std::list using a function predicate,
something like:

bool predicate( const& M m1, const& M m2 ) {
   return m1.somedata < m2.somedata;
}

I tried to call std::list::sort with such a predicate but that doesn't work:

mylist.sort( predicate ); // error, no matching function call

So since I must not call std::sort on a list, how can I sort it in a way
other than the standard operator< of its element does?
I need to do it with predicates, because I need to sort the list with
different sorting criterions, depending on the situation.

Regards,
Matthias



Relevant Pages

  • Re: Name For - Functions that indicate order?
    ... earlier in a sequence and false if not. ... At the moment the function is called CompareItem, I am trying to come up ... Predicate used to sort entire vector". ...
    (comp.programming)
  • Re: randomize-list
    ... implementations of sort can either ... If the key and predicate always return, ... and does not require predicate or key to be consistent: ...
    (comp.lang.lisp)
  • Re: does sort always sort vectors in place?
    ... this is accomplished by permuting the elements in ... ??>> (setf v (sort v predicate)) ... Subtle but crucial difference. ...
    (comp.lang.lisp)
  • Am I abusing SORT? It sure aint working for me....
    ... According to the CLHS in re SORT: ... "Predicate should return true if and only if the first argument is strictly less than the second. ... My only requirement on the sort result is that no node precede any of its ancestors (its parent or its parent's parent or etc). ... So nil means either that arg2 is an ascendant of arg1, or that neither is an ascendant of the other. ...
    (comp.lang.lisp)
  • Re: does sort always sort vectors in place?
    ... this is accomplished by permuting the elements in ... ??>> (setf v (sort v predicate)) ... Subtle but crucial difference. ...
    (comp.lang.lisp)