Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to sort a 2D QVector<Qvector>double>> by the first column

How to sort a 2D QVector<Qvector>double>> by the first column

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tomax63
    wrote on last edited by
    #1

    My QVector<Qvector<double>> looks ike this:

    {{100, 50}, {150, 40}, {50, 58}, {75, 60}}

    I want it to look like

    {{50, 58}, {75, 60}, {100, 50}, {150, 40}}

    How can I sort it by the first column?

    Thomas

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      std::sort(
          vect.begin()
          ,vect.end()
          ,[](const QVector<double>& left,const QVector<double>& right)->bool{
              if(left.empty()) 
                  return true; 
              if(right.empty())
                  return true;
              return left.first()<right.first();
          }
      );
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • T Offline
        T Offline
        Tomax63
        wrote on last edited by Tomax63
        #3

        Wow, thats fast! Thanks!

            sort(erhalten.begin(),erhalten.end() ,[](const QVector<double>& left,const QVector<double>& right)->bool{
                    if(left.empty())
                        return true;
                    if(right.empty())
                        return true;
                    return left.first()<right.first();
                }
            );
        
        

        Sadly i get 7 error messages like:

        C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\stl_algo.h:1847: Fehler: passing 'const QVector<double>' as 'this' argument discards qualifiers [-fpermissive]
        *__first = _GLIBCXX_MOVE(__val);
        ^
        C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\stl_heap.h:218: Fehler: passing 'const QVector<double>' as 'this' argument discards qualifiers [-fpermissive]
        (__first + __holeIndex) = _GLIBCXX_MOVE((__first + __secondChild));
        ^
        C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\stl_heap.h:224: Fehler: passing 'const QVector<double>' as 'this' argument discards qualifiers [-fpermissive]
        (__first + __holeIndex) = _GLIBCXX_MOVE((__first
        ^

        1 Reply Last reply
        0
        • JohanSoloJ Offline
          JohanSoloJ Offline
          JohanSolo
          wrote on last edited by JohanSolo
          #4

          Remove the const then.

          `They did not know it was impossible, so they did it.'
          -- Mark Twain

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tomax63
            wrote on last edited by
            #5

            Nope, still more errors (10)

            C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\stl_algo.h:1847: Fehler: passing 'const QVector<double>' as 'this' argument discards qualifiers [-fpermissive]
            __first = _GLIBCXX_MOVE(__val);
            ^
            C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\predefined_ops.h:123: Fehler: no match for call to '(MainWindow::get_real(QVector<QVector<double> >)::<lambda(QVector<double>&, QVector<double>&)>) (const QVector<double>&, const QVector<double>&)'
            { return bool(_M_comp(
            __it1, __it2)); }
            ^
            C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\predefined_ops.h:123: Fehler: binding 'const QVector<double>' to reference of type 'QVector<double>&' discards qualifiers
            C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\predefined_ops.h:123: Fehler: binding 'const QVector<double>' to reference of type 'QVector<double>&' discards qualifiers
            { return bool(_M_comp(
            __it1, *__it2)); }
            ^

            and 6 more

            1 Reply Last reply
            0
            • JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by
              #6

              Ah, sorry, I feel you're calling the aforementioned code in a const method of MainWindow. Does this make sense?

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              1 Reply Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                To sort you need to modify the array. is erhalten const?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  Tomax63
                  wrote on last edited by
                  #8

                  I have no idea whether that makes sense. I just copied the code into my routine where i want to process the Qvector.

                  How else should I use the code?

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    Correction:

                    std::sort(erhalten.begin(),erhalten.end() ,[](const QVector<double>& left,const QVector<double>& right)->bool{
                                if(left.empty() && right.empty())
                                    return false;
                                if(left.empty())
                                    return true;
                                if(right.empty())
                                    return false;
                                return left.first()<right.first();
                            }
                        );
                    

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1
                    • T Offline
                      T Offline
                      Tomax63
                      wrote on last edited by
                      #10

                      Yes, that was the problem. my array was const. Sorry for that easy error.

                      Thanks a lot!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved