fork download
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. const int COL = 2;
  5.  
  6. int compare_rows(const void* lhs, const void* rhs)
  7. {
  8. auto l = static_cast<const int(*)[COL]>(lhs);
  9. auto r = static_cast<const int(*)[COL]>(rhs);
  10. return (*r)[COL-1] - (*l)[COL-1];
  11. }
  12.  
  13. int main()
  14. {
  15. int Arr[3][COL] = {{1, 5}, {2, 8}, {3, 3}};
  16.  
  17. std::qsort(Arr, 3, sizeof Arr[0], compare_rows);
  18.  
  19. // IDEone still has an outdated compiler, no range loops
  20. // for(auto& row: Arr)
  21. // {
  22. // for(auto& n: row)
  23. // std::cout << n << ' ';
  24. // std::cout << '\n';
  25. // }
  26. for(int row = 0; row < 3; ++row)
  27. {
  28. for(int col = 0; col < COL; ++col)
  29. std::cout << Arr[row][col] << ' ';
  30. std::cout << '\n';
  31. }
  32. }
  33.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
2 8 
1 5 
3 3