Possible Duplicate:
How to use std::sort with a vector of structures and compare function?
I have a cat object (what?) and a catSort object which obviously sorts the cat objects. Below is the classes
class cat { public: int age; }; class catSorter { public: vector< cat > cats; vector< cat > SortCatsByAge(); void AddCat( cat new_cat ); }; void catSorter::AddCat(cat new_cat){ this->cats.push_back(new_cat) } vector< cat > catSorter::SortCatsByAge(){ // Sort cats here by age! } cat tim; tim.age = 10; cat mark; mark.age = 20 cat phil; phil.age = 3; catSorter sorter; sorter->AddCat(tim); sorter->AddCat(mark); sorter->AddCat(phil); std::<vector> sortedcats = sorter->SortCatsByAge();
I'm having difficulties sorting a vector, how would I go about doing this? Should I just loop through the cats
attribute and store them inside a temporary vector then return that? Is there an easier way to do this?