none
sort multidimensional array

    Question

  • Hi. I am a beginner  in Visual C++ and have confused for sorting the multidimensional array because there are a lot of ways to do that. Please help.

    I have an array with 51 rows and 4 columns like : int Myarray[51][4]. I want to sort it based on the second column. What is the best way to do that with the least code? (I know there are sort() and qsort() but I do not know how to use it). please tell the header it needs too.

    regards.


    • Edited by salmasa150 Sunday, July 28, 2013 7:24 AM
    Sunday, July 28, 2013 7:24 AM

Answers

  • What exactly I want is as follow.

    I want to sort an array like Myarray[3][4] based on the second column while other corresponding 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.

    If you express your array as a std::vector<std::vector<int>> then you can use the following comparison object

    typedef std::vector<int> IntVec;

    struct Comp
    {
        bool operator()(const IntVec& a, const IntVec& b) const
        {
            return a[1] < b[1];
        }
    };


    David Wilkinson | Visual C++ MVP

    Sunday, July 28, 2013 1:26 PM
  • Myarray is an array with 51 elements.  (The fact that each element is itself an array of 4 int is, for the moment, a nit).

    To sort the elements using qsort:

        You would pass qsort the address of the first element (either Myarray or &Myarray[0], the two are equivalent in this context), the number of elements (some expression evaluating to 51), the size of each element (sizeof Myarray[0]), and a pointer to the comparison function.

        qsort will call the comparison function with the addresses of two elements.  (Now the nit becomes significant.)  The comparison function must answer the question "Is element 1 less than, equal to, or greater than element 2?".  (It does this by returning a negative, 0, or positive value, respecively.)  Since each element is an array of 4 int and since the addresses passed to the function have type void*, the first thing to do is to convert the addresses to type int*.  Now each address points to the first int (of the 4) in each element.  It is now trivial to use these addresses to access in each element the particular int relevant for the comparison.

    Sunday, July 28, 2013 5:58 PM

All replies

  • Hi. I am a beginner  in Visual C++ and have confused for sorting the multidimensional array because there are a lot of ways to do that. Please help.

    I have an array with 51 rows and 4 columns like : int Myarray[51][4]. I want to sort it based on the second column. What is the best way to do that with the least code? (I know there are sort() and qsort() but I do not know how to use it). please tell the header it needs too.

    If you use std::sort, you can provide a comparison function that will allow you to sort any desired type. See

    http://www.cplusplus.com/reference/algorithm/sort/

    std::sort requires the <algorithm> header.


    David Wilkinson | Visual C++ MVP

    Sunday, July 28, 2013 11:12 AM
  • What exactly I want is as follow.

    I want to sort an array like Myarray[3][4] based on the second column while other corresponding 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.

    Sunday, July 28, 2013 12:30 PM
  • What exactly I want is as follow.

    I want to sort an array like Myarray[3][4] based on the second column while other corresponding 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.

    If you express your array as a std::vector<std::vector<int>> then you can use the following comparison object

    typedef std::vector<int> IntVec;

    struct Comp
    {
        bool operator()(const IntVec& a, const IntVec& b) const
        {
            return a[1] < b[1];
        }
    };


    David Wilkinson | Visual C++ MVP

    Sunday, July 28, 2013 1:26 PM
  • Something like this:

    #include <algorithm>
    
    struct OneRow {
      int row[4];
    };
    
    class Compare {
      int col_;
    public:
      explicit Compare(int col) : col_(col) {}
      bool operator()(const OneRow& l, const OneRow& r) const {
        return l.row[col_] < r.row[col_];
      }
    };
    
    int Myarray[3][4] = {
      {3, 8, 7, 2},
      {9, 12, 0, 4},
      {12, 2, 14, 1}
    };
    
    int main() {
      OneRow* rows = reinterpret_cast<OneRow*>(Myarray);
      std::sort(rows, rows + 3, Compare(1));
      return 0;
    }

    This relies on a hack though - that reinterpret_cast technically exhibits undefined behavior. std::sort won't work on arrays directly - arrays aren't really first-class objects in C++; in particular, they can't be copied or assigned.

    With modern C++, it's best to use std::array (std::tr1::array for VS versions before VS2012) for fixed size arrays. Applying std::sort to an array<array<int, 4>, 3> would be straightforward.


    Igor Tandetnik


    • Edited by Igor TandetnikMVP Sunday, July 28, 2013 5:18 PM Correcting minor technical error
    Sunday, July 28, 2013 4:55 PM
  • Myarray is an array with 51 elements.  (The fact that each element is itself an array of 4 int is, for the moment, a nit).

    To sort the elements using qsort:

        You would pass qsort the address of the first element (either Myarray or &Myarray[0], the two are equivalent in this context), the number of elements (some expression evaluating to 51), the size of each element (sizeof Myarray[0]), and a pointer to the comparison function.

        qsort will call the comparison function with the addresses of two elements.  (Now the nit becomes significant.)  The comparison function must answer the question "Is element 1 less than, equal to, or greater than element 2?".  (It does this by returning a negative, 0, or positive value, respecively.)  Since each element is an array of 4 int and since the addresses passed to the function have type void*, the first thing to do is to convert the addresses to type int*.  Now each address points to the first int (of the 4) in each element.  It is now trivial to use these addresses to access in each element the particular int relevant for the comparison.

    Sunday, July 28, 2013 5:58 PM