Skip to content

Instantly share code, notes, and snippets.

@chengluyu
Last active December 24, 2015 15:39
Show Gist options
  • Save chengluyu/6821743 to your computer and use it in GitHub Desktop.
Save chengluyu/6821743 to your computer and use it in GitHub Desktop.
Multi-key sort
// Type definition
class Object {
public:
int key1, key2, key3, key4;
};
// Comparator
bool comparator(const Object & lhs, const Object & rhs) {
if (lhs.key1 == rhs.key1) {
if (lhs.key2 == rhs.key2) {
if (lhs.key3 == rhs.key3) {
return lhs.key4 < rhs.key4;
}
return lhs.key3 < rhs.key3;
}
return lhs.key2 < rhs.key2;
}
return lhs.key1 < rhs.key1;
}
// Sort
vector<Object> objs;
// do something with objs
sort(objs.begin(), objs.end(), comparator);
@chengluyu
Copy link
Author

多关键字排序,关键字次序:key1 > key2 > key3 > key4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment