#include <iostream>
using namespace std;
#include <string>
#include<list>
class Person {
public:
int age;
int height;
string name;
public:
Person(string name, int age, int height) {
this->age = age;
this->name = name;
this->height = height;
}
};
bool mycompare(Person & p1,Person & p2) {
if (p1.age == p2.age) {
//然后年龄相同的话,按身高降序排,所以是高的在前面且是p1.height>p2.height
return p1.height > p2.height;
}
else {
//先按年龄升序排列,所以小的在前面 所以是p1.age<p2.age
return p1.age < p2.age;
}
}
void printList(const list<Person> & v1) {
for (list<Person>::const_iterator it = v1.begin(); it != v1.end(); it++) {
cout << "姓名是" << (*it).name << "年龄是:" << it->age << "身高是" << it->height <<endl;
}
cout << endl;
}
void test01() {
list<Person> L1;
Person p1("张飞", 20, 190);
Person p2("李逵", 21, 188);
Person p3("刘备", 35, 175);
Person p4("AAA", 27, 165);
Person p5("BBB", 27, 185);
Person p6("CCC", 30, 175);
L1.push_back(p1);
L1.push_back(p2);
L1.push_back(p3);
L1.push_back(p4);
L1.push_back(p5);
L1.push_back(p6);
printList(L1);
L1.sort(mycompare);
cout << "排序后" << endl;
printList(L1);
}
int main() {
test01();
system("pause");
return 0;
}