Skip to content

Instantly share code, notes, and snippets.

@robwhess
Created January 23, 2017 05:20
Show Gist options
  • Save robwhess/0242151fd70de87507769b7b6472e841 to your computer and use it in GitHub Desktop.
Save robwhess/0242151fd70de87507769b7b6472e841 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
struct state {
std::string name;
float unemployed_2007;
float unemployed_2015;
};
bool cmp_unemploy_asc(struct state lhs, struct state rhs) {
return (lhs.unemployed_2015 - lhs.unemployed_2007) <
(rhs.unemployed_2015 - rhs.unemployed_2007);
}
bool cmp_unemploy_desc(struct state lhs, struct state rhs) {
return (lhs.unemployed_2015 - lhs.unemployed_2007) >
(rhs.unemployed_2015 - rhs.unemployed_2007);
}
int main(int argc, char** argv) {
struct state* states = new struct state[3];
states[0].name = "Oregon";
states[0].unemployed_2007 = 5.2;
states[0].unemployed_2015 = 5.7;
states[1].name = "California";
states[1].unemployed_2007 = 5.4;
states[1].unemployed_2015 = 6.2;
states[2].name = "Pennsylvania";
states[2].unemployed_2007 = 4.4;
states[2].unemployed_2015 = 5.1;
std::cout << "Before sort:" << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << " " << states[i].name << "\t"
<< states[i].unemployed_2015 - states[i].unemployed_2007 << std::endl;
}
std::sort(states, states + 3, cmp_unemploy_desc);
std::cout << "After descending sort:" << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << " " << states[i].name << "\t"
<< states[i].unemployed_2015 - states[i].unemployed_2007 << std::endl;
}
std::sort(states, states + 3, cmp_unemploy_asc);
std::cout << "After ascending sort:" << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << " " << states[i].name << "\t"
<< states[i].unemployed_2015 - states[i].unemployed_2007 << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment