Solved How to sort a 2D QVector<Qvector>double>> by the first column
-
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
-
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(); } );
-
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
^ -
Remove theconst
then. -
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
-
Ah, sorry, I feel you're calling the aforementioned code in a const method of
MainWindow
. Does this make sense? -
To sort you need to modify the array. is
erhalten
const? -
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?
-
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(); } );
-
Yes, that was the problem. my array was const. Sorry for that easy error.
Thanks a lot!