Last active
December 24, 2015 15:39
-
-
Save chengluyu/6821743 to your computer and use it in GitHub Desktop.
Multi-key sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
多关键字排序,关键字次序:
key1 > key2 > key3 > key4
。