Page 1 of 1

sorting list

Posted: Wed Sep 28, 2005 8:44 pm
by Project
Hello!

I'm using VC 6.0. But when I try to sort a list with comp-function I have problems with compilation of source.

here is my code:

Code: Select all

	//comp-function:
	bool comp(int a, int b)
	{
		return (a>b);
	}

...
//in main():
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);

l.sort(comp); // !!! Here I have an error... But why? How can I use this?
However, my friend compiled my code successfully on vc7 and gcc.
But how can I use it with VC 6.0?
Help me, please.

Posted: Fri Sep 30, 2005 6:27 am
by sumankar
In order for sort to work overload the '<' operator.
Maybe...

Code: Select all

bool operator<(list<int>::iterator a, list<int>::iterator b)
{
       return (*a > *b);
}
...
//call it like 
l.sort()
...

Posted: Fri Sep 30, 2005 1:51 pm
by Project
2 sumankar: Great Thanks for the Help!!!