How to sort integer array[n][m]

What exactly I want is as follow.

I want to sort an array like Myarray[3][4] based on the second column while other correspondent rows exchange consequently.

Myarray is as follows:

3 8 7 2

9 12 0 4

12 2 14 1

I want to sort it based on the second column using “std::qsort" or "std::sort” or other quick method that would lead to the following :

12 2 14 1

3 8 7 2

9 12 0 4



It would be appreciated if somebody helps me.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <iomanip>

int main()
{
    std::vector< std::vector<int> > my_array = { { 3, 8, 7, 2 }, { 9, 12, 0, 4 }, { 12, 2, 14, 1 } } ;

    std::sort( std::begin(my_array), std::end(my_array),
               []( const std::vector<int>& a, const std::vector<int>& b ) { return a[1] < b[1] ; } ) ;

    for( const auto& row : my_array )
    {
        for( int v : row ) std::cout << std::setw(3) << v ;
        std::cout << '\n' ;
    }
}

http://ideone.com/ABy7VS
Thank you a lot.
There is only only on another question.
Assuming to have an filled array of integer, how can I convert it into a vector in line 9.
Regards.
I compiled it in C++2012 and have the following error.


1>------ Build started: Project: 3, Configuration: Debug Win32 ------
1> Source4.cpp
1>c:\users\art\documents\visual studio 2012\projects\3\3\source4.cpp(10): error C2552: 'my_array' : non-aggregates cannot be initialized with initializer list
1> 'std::vector<_Ty>' : Types with a base are not aggregate
1> with
1> [
1> _Ty=std::vector<int>
1> ]
1>c:\users\art\documents\visual studio 2012\projects\3\3\source4.cpp(10): error C2078: too many initializers
1>c:\users\art\documents\visual studio 2012\projects\3\3\source4.cpp(10): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
> I compiled it in C++2012 and have the following error.

Yes, constructor initializer lists are not supported by Microsoft's original 2012 offering.


> Assuming to have an filled array of integer, how can I convert it into a vector in line 9.

This should compile cleanly with VS 2012:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <iomanip>

int main()
{
    const int my_array[3][4] = { { 3, 8, 7, 2 }, { 9, 12, 0, 4 }, { 12, 2, 14, 1 } } ;

    std::vector< std::vector<int> > my_vector ;
    for( const auto& row : my_array ) my_vector.push_back( std::vector<int>( std::begin(row), std::end(row) ) ) ;

    std::sort( std::begin(my_vector), std::end(my_vector),
               []( const std::vector<int>& a, const std::vector<int>& b ) { return a[1] < b[1] ; } ) ;

    for( const auto& row : my_vector )
    {
        for( int v : row ) std::cout << std::setw(3) << v ;
        std::cout << '\n' ;
    }
}
Excellent respond. Thank you a lot. You did a great job for me.
Just some other questions.
1- if I want to copy the sorted data of my_vector into my_array again in the end of program (line 21) what should I do?
2- why the size of my_vector is equal to size of my_array but the capacity of my_vector is more than its size.
3-the lines 11 and 12 in your last response is for transferring my_array[3][4] into my_vector[3][4]?
4- what is best: a-work with vectors in all of my program and forget the arrays or b- using the arrays and transfer it into vector and return it back when need
Regards.
Last edited on
what is best: a-work with vectors in all of my program and forget the arrays or b- using the arrays and transfer it into vector and return it back when need

Just work with vectors. They're almost always a better option than C-style arrays.
> 1- if I want to copy the sorted data of my_vector into my_array again in the end of program (line 21) what should I do?

1
2
for( std::size_t r = 0 ; r < my_vector.size() ; ++r )
    std::copy( std::begin( my_vector[r] ), std::end( my_vector[r] ), my_array[r] ) ;



> 2- why the size of my_vector is equal to size of my_array but the capacity of my_vector is more than its size.

See: http://stdcxx.apache.org/doc/stdlibug/5-2.html#524


> 3-the lines 11 and 12 in your last response is for transferring my_array[3][4] into my_vector[3][4]?

Yes.


> 4- what is best: a-work with vectors in all of my program and forget the arrays

Yes. Even if the size of the array is a constant known at compile time, the simple value semantics of std::vector<> make the code for sorting (moving rows around) much more natural.
Topic archived. No new replies allowed.