1 Replies - 139 Views - Last Post: 03 September 2019 - 08:04 PM Rate Topic: -----

#1 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 184
  • Joined: 15-January 13

Sort vector of objects in c++ using "sort" function.

Posted 03 September 2019 - 07:18 PM

Hi!

I'm trying to use "sort" function in c++ to sort a vector. In my case I know how to do that when I want to sort the following objects by age. I overload the "<"" operator.
But my question is if I want to sort by weight too?

person.h
class Person {
private:
	string name;
	int age;
	float weight;

	/*implementations here*/
};

bool operator<(Person &p1, Person &p2);



person.cpp
bool operator<(Person &p1, Person &p2){
	return p1.getAge() > p2.getAge();
}


main.cpp
int main(int argc, char *argc[]) {
	vector<Person> vecPerson;
	//...

	sort(vecPerson.begin(), vecPerson.end());

	//Now I want to sort by weight.

}


Is This A Good Question/Topic? 0
  • +

Replies To: Sort vector of objects in c++ using "sort" function.

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2381
  • View blog
  • Posts: 4,516
  • Joined: 30-May 10

Re: Sort vector of objects in c++ using "sort" function.

Posted 03 September 2019 - 08:04 PM

http://www.cplusplus...algorithm/sort/
Pass the sort function explicitly as the 3rd parameter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1