I suggest this only because it was one of the few things that std::qsort does well, that std::sort just does not, namely, it sorts multi-table fixed arrays: the comparator is a string of ternary operators, but should be clear enough if you look at it long enough:
#include <iostream> #include <random> #include <algorithm> int main() { int ar[10][2]; // populate with random data std::random_device rd; std::default_random_engine rng(rd()); std::uniform_int_distribution<> dist(1,20); std::for_each(std::begin(ar), std::end(ar), [&](int (&ar)[2]){ ar[0] = dist(rng); ar[1] = dist(rng); }); std::cout << "Before Sort..." << '\n'; std::for_each(std::begin(ar), std::end(ar), [](const int(&ar)[2]) { std::cout << ar[0] << ',' << ar[1] << '\n';}); std::qsort(ar, 10, sizeof(*ar), [](const void *arg1, const void *arg2)->int { int const *lhs = static_cast<int const*>(arg1); int const *rhs = static_cast<int const*>(arg2); return (lhs[0] < rhs[0]) ? -1 : ((rhs[0] < lhs[0]) ? 1 : (lhs[1] < rhs[1] ? -1 : ((rhs[1] < lhs[1] ? 1 : 0)))); }); std::cout << "After Sort..." << '\n'; std::for_each(std::begin(ar), std::end(ar), [](const int(&ar)[2]) { std::cout << ar[0] << ',' << ar[1] << '\n';}); return 0; }
Run example (yours will be different, obviously)
Before Sort... 2,11 18,4 20,20 14,6 8,10 17,8 14,14 3,10 20,14 19,19 After Sort... 2,11 3,10 8,10 14,6 14,14 17,8 18,4 19,19 20,14 20,20
Notes: this specifically uses string comparison rather than subtracting short cuts in the comparator to avoid potential flow problems. If this is not a problem in your limited data space, you can easily make this comparator much simpler.