Sort a vector of pairs by first element then by second element of the pair in C++? [duplicate]
This question already has an answer here: Sorting a std::vector<std::pair<std::string,bool>> by the string? 4 answers If I have a vector<pair<int,int> > datatype, what is the accepted way to sort it by the first element of the pair and then by second if the firsts are equal? For instance maybe (1,10), (3,3), (7,13), (7,16), (8,1), (8,2), (15,2) etc. pair s by default compare by first element, then second. So, if you don't care about preserving the order when the first elements compare equal, then you can just use std::sort : std::sort(v.begin(), v.end()); std::pair s comparison operators