arranging vector<object*> in order

Hi I have created an taxpayer object which has string name, and a float salary, and i store them as pointers in a vector<taxpayer*>. Can anybody tell me how to arrange the taxpayers in order of their salary size so that when i print them out it prints the taxpayer with the largest salary first and smallest last?

any help is much appreciated.

Thank You
Write a comparator function for pointer to taxpayer and use std::sort http://www.cplusplus.com/reference/algorithm/sort/
Thank you Bazzy.

should the comparator function look this :

bool myfunction (taxpayer* i,taxpayer* j)
{
return (i->tax_amount < j->tax_amount);
}

Thanks again.
That looks like the general idea to me although the class attributes probably shouldn't be public. I would have written const accessor functions and kept the attributes private. Try compiling it and using it and see what happens. The name of the function is the third arg of std::sort. If it is nested within the class as a static member function then simply qualify the name with the class scope as well. There are many specific ways to accomplish this but what you are doing seems okay.
Topic archived. No new replies allowed.