#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
enum type { id = 0, name = 1 };
enum order { inc = 0, dec = 1 };
class Person {
friend bool name_sort_less(const Person& a, const Person& b);
friend bool name_sort_greater(const Person& a, const Person& b);
friend void print(const Person& per);
public:
Person(const string& strName, double fPay) : name(strName), pay(fPay) { }
bool operator<(const Person& per) const {
return pay < per.pay;
}
bool operator>(const Person& per) const {
return pay > per.pay;
}
private:
string name;
double pay;
};
bool name_sort_less(const Person& a, const Person& b) {
return a.name < b.name;
}
bool name_sort_greater(const Person& a, const Person& b) {
return a.name > b.name;
}
void print(const Person& per) {
cout << per.name << " " << per.pay << endl;
}
void init(vector<Person>& v) {
v.emplace_back(Person("Jack ", 6700.54f));
v.emplace_back(Person("Jimmy", 2850.01f));
v.emplace_back(Person("Mary ", 5132.22f));
v.emplace_back(Person("Jessy", 11255.50f));
v.emplace_back(Person("Ander", 1253.27f));
}
void sort(type t, order o, vector<Person>& v) {
if (t == type::id && o == order::inc)
sort(v.begin(), v.end(), less<Person>());
else if(t == type::name && o == order::inc)
sort(v.begin(), v.end(), greater<Person>());
else if(t == type::id && o == order::dec)
sort(v.begin(), v.end(), name_sort_less);
else if(t == type::name && o == order::dec)
sort(v.begin(), v.end(), name_sort_greater);
}
void travel(const vector<Person>& v) {
for_each(v.begin(), v.end(), print);
}
int main() {
vector<Person> v;
init(v);
cout << "before sort..." << endl;
travel(v);
sort(type::id, order::inc, v);
cout << "after sort (increase)..." << endl;
travel(v);
sort(type::id, order::dec, v);;
cout << "after sort (decrease)..." << endl;
travel(v);
cout << "before sort..." << endl;
travel(v);
sort(type::name, order::inc, v);
cout << "after sort (increase)..." << endl;
travel(v);
cout << "before sort..." << endl;
travel(v);
sort(type::name, order::dec, v);
cout << "after sort (decrease)..." << endl;
travel(v);
return 0;
}