Sprawdź to: How to sort multidimensional array by column in C++?
szczególnie ten przykład. Przerobiłem go "trochę" , żeby dopasować do:
konkretnie trójek tylko po pierwszej kolumnie, np:
1 2 4 , 8 3 2 , 4 2 1 , 2 5 7
na 1 2 4 , 2 5 7 , 4 2 1 , 8 3 2.
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
int main()
{
std::array<int, 3> data[] =
{
{ { 1, 2, 4 } },
{ { 8, 3, 2 } },
{ { 4, 2, 1 } },
{ { 2, 5, 7 } }
};
for ( const auto &row : data )
{
for ( const auto &s : row ) {
std::cout.width(2);
std::cout << s << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
std::sort( std::begin( data ), std::end( data ),
[]( const auto &a, const auto &b )
{
return a[0] < b[0];
} );
for ( const auto &row : data )
{
for ( const auto &s : row ) {
std::cout.width(2);
std::cout << s << ' ';
}
std::cout << std::endl;
}
}