list.sort(compfn) 
Author Message
 list.sort(compfn)

Can anyone show me an example of a compFn for a list sort
method (when the < operator has already been overloaded
for another search) ?

I have a class of object called CBox's and I want to sort
by width, so I created a

bool operator<(CBox& a, CBox& b)
{
  return ( a.width() < b.width() );

Quote:
}

I call list.sort() and everything works fine.

But now I need an additional sort by height. I can't
overload the < again. So how do I create the binary
predicate(?) for the list sort.

i.e.  list.sort(ByHeight).

Thanks in advance,

Rob Adelberg



Tue, 05 Apr 2005 23:26:48 GMT  
 list.sort(compfn)
bool byHeight(const CBox& a, const CBox& b)
{
    return ( a.height() < b.height() );
Quote:
}

myList.sort(byHeight);

Or

struct ByHeight
{
    bool operator() (const CBox& a, const CBox& b) const
    {
        return ( a.height() < b.height() );
    }

Quote:
};

myList.sort(ByHeight());

If you are using VC6 with STL as shipped, you will need to modify <list>
header for this to work. Open the header (note - it is <list> without
extension, not <list.h>) and replace

 void sort(_Pr3 _Pr)

on line 360 with

 template <class _Pr3>
 void sort(_Pr3 _Pr)

and

 void merge(_Myt& _X, _Pr3 _Pr)

on line 325 with

 template <class _Pr3>
 void merge(_Myt& _X, _Pr3 _Pr)

--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Can anyone show me an example of a compFn for a list sort
> method (when the < operator has already been overloaded
> for another search) ?

> I have a class of object called CBox's and I want to sort
> by width, so I created a

> bool operator<(CBox& a, CBox& b)
> {
>   return ( a.width() < b.width() );
> }
> I call list.sort() and everything works fine.

> But now I need an additional sort by height. I can't
> overload the < again. So how do I create the binary
> predicate(?) for the list sort.

> i.e.  list.sort(ByHeight).

> Thanks in advance,

> Rob Adelberg



Tue, 05 Apr 2005 23:41:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. List files...and sort by name, date, size

2. Link-list sort algorithm question

3. basic sort for singly linked list...

4. deleting and sorting items in a linked list

5. sorting of a linked list

6. Linked List Sort/Search/delete code ??Help????

7. Linked list sorting...

8. Hashtable Sorted list

9. quick-sorting a linked list?

10. Please Help: Sorting a linked list

11. Fast N lg N sort algorithm for lists - source code available

12. Sorting on a doubly linked list

 

 
Powered by phpBB® Forum Software