<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to sort QList&lt;QList&lt;QVariant&gt;&gt;]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I have list of lists such QList&lt;QList&lt;QVariant&gt;&gt;, I'd like to know how one can sort on the entire list by a certain index.</p>
<p dir="auto">So if the top level Qlist has 100 entries (say 100 rows) and each row has 10 entries each (say 10 columns), I'd like to sort the entire list by the second column.  I looked around but I only see one dimensional sorts not two.</p>
<p dir="auto">The reason I'm asking is I'm using a QAbstractTableModel with a view and when I sort the view using a proxyModel the data source is not in sync with the sort so of course the display is not what I expected.  I was wondering, perhaps I just sort on the data source and it should automatically display properly on the view and even get rid of the proxyModel.</p>
<p dir="auto">If anyone knows of a better way, please let me know or please point me in a direction to sort the data source.</p>
<p dir="auto">Thanks!</p>
]]></description><link>https://forum.qt.io/topic/110231/how-to-sort-qlist-qlist-qvariant</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Jul 2026 23:16:53 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/110231.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 31 Dec 2019 19:25:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Mon, 06 Jan 2020 09:23:22 GMT]]></title><description><![CDATA[<p dir="auto">I just inverted the 2 siders of <code>:</code>, it's trivial to fix:</p>
<pre><code class="language-cpp">void MyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder){
emit layoutAboutToBeChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
std::sort(list.begin(),list.end(),[column,order](const QList&lt;QVariant&gt;&amp; a, const QList&lt;QVariant&gt;&amp; b)-&gt;bool{
const auto aV = a.at(column);
const auto bV = b.at(column);
if(order == Qt::AscendingOrder)
return aV==bV ?  a.at(2)&lt;b.at(2) : aV&lt;bV;
return aV==bV ? a.at(2)&gt;b.at(2) : aV&gt;bV;
});
emit layoutChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
}

</code></pre>
]]></description><link>https://forum.qt.io/post/570628</link><guid isPermaLink="true">https://forum.qt.io/post/570628</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Mon, 06 Jan 2020 09:23:22 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Sat, 04 Jan 2020 21:24:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> said in <a href="/post/570329">How to sort QList&lt;QList&lt;QVariant&gt;&gt;</a>:</p>
<blockquote>
<p dir="auto">mit layoutAboutToBeChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);<br />
std::sort(list.begin(),list.end(),[column,order](const QList&lt;QVariant&gt;&amp; a, const QList&lt;QVariant&gt;&amp; b)-&gt;bool{<br />
const auto aV = <a href="http://a.at" target="_blank" rel="noopener noreferrer nofollow ugc">a.at</a>(column);<br />
const auto bV = <a href="http://b.at" target="_blank" rel="noopener noreferrer nofollow ugc">b.at</a>(column);<br />
if(order == Qt::AscendingOrder)<br />
return aV==bV ? aV&lt;bV : <a href="http://a.at" target="_blank" rel="noopener noreferrer nofollow ugc">a.at</a>(2)&lt;<a href="http://b.at" target="_blank" rel="noopener noreferrer nofollow ugc">b.at</a>(2);<br />
return aV==bV ? aV&gt;bV : <a href="http://a.at" target="_blank" rel="noopener noreferrer nofollow ugc">a.at</a>(2)&gt;<a href="http://b.at" target="_blank" rel="noopener noreferrer nofollow ugc">b.at</a>(2);<br />
});<br />
emit layoutChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);</p>
</blockquote>
<p dir="auto">Unfortunately this does not work.<br />
According to the code you have we don't sort on column at all.  We first have to sort on column and once that is done, then we resort all duplicates of column values to some other column.</p>
]]></description><link>https://forum.qt.io/post/570467</link><guid isPermaLink="true">https://forum.qt.io/post/570467</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Sat, 04 Jan 2020 21:24:47 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Sat, 04 Jan 2020 20:42:41 GMT]]></title><description><![CDATA[<p dir="auto">Thank you!  I'll try it.</p>
]]></description><link>https://forum.qt.io/post/570455</link><guid isPermaLink="true">https://forum.qt.io/post/570455</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Sat, 04 Jan 2020 20:42:41 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Fri, 03 Jan 2020 20:06:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/leinad">@<bdi>leinad</bdi></a> said in <a href="/post/570303">How to sort QList&lt;QList&lt;QVariant&gt;&gt;</a>:</p>
<blockquote>
<p dir="auto">For some reason QStandardItemModel handles this all by itself</p>
</blockquote>
<p dir="auto">It doesn't. It probably just uses <code>std::stable_sort</code> instead of <code>std::sort</code>.</p>
<p dir="auto">In any case this case is trivial, the comparison function can take care of it.<br />
Using your example:</p>
<pre><code class="language-cpp">void MyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder){
emit layoutAboutToBeChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
std::sort(list.begin(),list.end(),[column,order](const QList&lt;QVariant&gt;&amp; a, const QList&lt;QVariant&gt;&amp; b)-&gt;bool{
const auto aV = a.at(column);
const auto bV = b.at(column);
if(order == Qt::AscendingOrder)
return aV==bV ? aV&lt;bV : a.at(2)&lt;b.at(2);
return aV==bV ? aV&gt;bV : a.at(2)&gt;b.at(2);
});
emit layoutChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/570329</link><guid isPermaLink="true">https://forum.qt.io/post/570329</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 03 Jan 2020 20:06:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Fri, 03 Jan 2020 15:07:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> said in <a href="/post/570239">How to sort QList&lt;QList&lt;QVariant&gt;&gt;</a>:</p>
<blockquote>
<p dir="auto">emit layoutChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);</p>
</blockquote>
<p dir="auto">VRonin,</p>
<p dir="auto">I have another question.  Would you know how to handle this case in a sort?  It seems to work okay when using QStandardItemModel sorting under Qt  but not with the above sorting.</p>
<p dir="auto">So the sorting works fine as is where we sort properly on column 1 which gives us the bottom results.  But now within that sort I'd like to sort name1 further using column2 so name1 and name 2 is rearranged in column2 order.<br />
If column1 is not a duplicate within the column 0 grouping then no further sort is required.  So essentially take the first sort and for duplicate column 0 and column1 values resort using column 2.  Is this possible?</p>
<p dir="auto">Example:<br />
Think of column 1 as time (consider a string) where we have row data for the same object name with duplicate time, and column 2 is the rowID from a database.  So even though technically the sort is correct by time the exact order in the database is governed by the rowID from a database.  Essentially sort by time and then by RowID for objects in column0 and 1 that  have the same value.  For some reason QStandardItemModel handles this all by itself so it would be nice if I can recreate it.</p>
<p dir="auto">column 0    column1  column2<br />
name1        123              3<br />
name1        123              1<br />
name1        123              2<br />
name2         345             2<br />
name2         345             3<br />
name2         345             1</p>
<p dir="auto">result:<br />
name1        123              1<br />
name1        123              2<br />
name1        123              3<br />
name2         345             1<br />
name2         345             2<br />
name2         345             3</p>
]]></description><link>https://forum.qt.io/post/570303</link><guid isPermaLink="true">https://forum.qt.io/post/570303</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Fri, 03 Jan 2020 15:07:36 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Fri, 03 Jan 2020 14:10:09 GMT]]></title><description><![CDATA[<p dir="auto">That is exactly what I did except for:</p>
<p dir="auto">emit layoutAboutToBeChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);</p>
]]></description><link>https://forum.qt.io/post/570294</link><guid isPermaLink="true">https://forum.qt.io/post/570294</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Fri, 03 Jan 2020 14:10:09 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Fri, 03 Jan 2020 09:34:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/leinad">@<bdi>leinad</bdi></a> said in <a href="/post/570120">How to sort QList&lt;QList&lt;QVariant&gt;&gt;</a>:</p>
<blockquote>
<p dir="auto">Now I need to create a signal and slot mechanism to check when a user clicks on a particular column on the view  for sorting which I will implement via std::sort.</p>
</blockquote>
<p dir="auto">No need to create anything fancy, just reimplement <code>QAbstractItemModel::sort</code> do do the sorting you want and everything else will be handled automatically by Qt</p>
<pre><code class="language-cpp">void MyModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder){
emit layoutAboutToBeChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
std::sort(list.begin(),list.end(),[column,order](const QList&lt;QVariant&gt;&amp; a, const QList&lt;QVariant&gt;&amp; b)-&gt;bool{
if(order == Qt::AscendingOrder)
return a.at(column)&lt;b.at(column);
return a.at(column)&gt;b.at(column);
});
emit layoutChanged({QModelIndex()}, QAbstractItemModel::VerticalSortHint);
}
</code></pre>
]]></description><link>https://forum.qt.io/post/570239</link><guid isPermaLink="true">https://forum.qt.io/post/570239</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 03 Jan 2020 09:34:55 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Thu, 02 Jan 2020 16:41:49 GMT]]></title><description><![CDATA[<p dir="auto">Well, it looks like Vronin solution worked.  I got rid of the proxyModel and attached the view directly to QAbstractTableModel and implemented the sort after the load.  It loads and sort very quickly.  Now I need to create a signal and slot mechanism to check when a user clicks on a particular column on the view  for sorting which I will implement via std::sort.</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/post/570120</link><guid isPermaLink="true">https://forum.qt.io/post/570120</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Thu, 02 Jan 2020 16:41:49 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Thu, 02 Jan 2020 14:37:10 GMT]]></title><description><![CDATA[<p dir="auto">Thanks.  Is there anyway have it sort at the end of the load rather than as it goes?  Why is it when I used to use QStandardItemModel and sorted using the treeView it was so much faster?  The load was fast too.  What happens under the covers using QstandardItemModel that makes such a difference?</p>
]]></description><link>https://forum.qt.io/post/570089</link><guid isPermaLink="true">https://forum.qt.io/post/570089</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Thu, 02 Jan 2020 14:37:10 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Thu, 02 Jan 2020 14:02:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/leinad">@<bdi>leinad</bdi></a> said in <a href="/post/570076">How to sort QList&lt;QList&lt;QVariant&gt;&gt;</a>:</p>
<blockquote>
<p dir="auto">So why when assigning a QSortFilterProxyModel to a view is loading the model so slow?  Does the proxymodel index the data somehow?</p>
</blockquote>
<p dir="auto">Yes, and it does sorting/filtering so it's slower.  I cannot say whether the slowness you see at 100,000 rows is right or not, I guess sorting that takes a bit of time.</p>
<p dir="auto">If you are happy to sort the underlying model explicitly and omit <code>QSortFilterProxyModel</code> you can do that, e.g. via the <code>std::sort()</code> <a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> has typed in.</p>
]]></description><link>https://forum.qt.io/post/570078</link><guid isPermaLink="true">https://forum.qt.io/post/570078</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 02 Jan 2020 14:02:55 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Thu, 02 Jan 2020 13:57:15 GMT]]></title><description><![CDATA[<p dir="auto">I tried using QSortFilterProxyModel but my issue is when I load thousands of rows, over time the load becomes slower and slower which is why I probabaly need to sort externally on my own.  If I don't use QSortFilterProxyModel and just assign my QAbstractTableModel to my view, the load is extremely fast.  So why when assigning a QSortFilterProxyModel to a view is loading the model so slow?  Does the proxymodel index the data somehow?</p>
<p dir="auto">Fast load:<br />
view-&gt;setSourceModel(QAbstractTableModel)</p>
<p dir="auto">slow load:<br />
proxyModel-&gt;setSourceModel(QAbstractTableModel);<br />
view-&gt;setModel(proxyModel)</p>
<p dir="auto">Are there any flags I should set prior to load in the proxy model to speed things up?</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/post/570076</link><guid isPermaLink="true">https://forum.qt.io/post/570076</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Thu, 02 Jan 2020 13:57:15 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Thu, 02 Jan 2020 13:41:30 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">if the top level Qlist has 100 entries (say 100 rows) and each row has 10 entries each (say 10 columns), I'd like to sort the entire list by the second column</p>
</blockquote>
<p dir="auto"><code>std::sort(list.begin(),list.end(),[](const QList&lt;QVariant&gt;&amp; a, const QList&lt;QVariant&gt;&amp; b)-&gt;bool{return a.at(1)&lt;b.at(1);});</code></p>
<blockquote>
<p dir="auto">QAbstractTableModel with a view and when I sort the view using a proxyModel the data source is not in sync with the sort so of course the display is not what I expected</p>
</blockquote>
<p dir="auto">This is not correct. the <code>QSortFilterProxyModel</code> is the one that takes care of keeping a mapping between the original model and what is displayed. It should work without any problem and show exactly what you expect</p>
]]></description><link>https://forum.qt.io/post/570072</link><guid isPermaLink="true">https://forum.qt.io/post/570072</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 02 Jan 2020 13:41:30 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Wed, 01 Jan 2020 08:55:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/leinad">@<bdi>leinad</bdi></a><br />
First you say your <code>QList</code> may have 100 rows, then you say it may have hundreds of thousands.  There is a difference, especially speed-wise!</p>
<p dir="auto">If I understand, your outer <code>QList</code> is a list of rows, and your inner <code>QList&lt;QVariant&gt;</code> is a list of the values in the columns for each row.</p>
<p dir="auto">I'm not sure which you <em>want</em> to achieve between just sorting the output (<code>QSortFiliterProxyModel</code>) versus actually sorting the data in the source model.  But whether you re-implement <code>[virtual]void QAbstractItemModel::sort(int column, Qt::SortOrder order = Qt::AscendingOrder</code> or you <code>qsort()</code> the outer <code>QList</code>, either way you will use the column number as index into the inner <code>QList&lt;QVariant&gt;</code> to pick out the column value to be compared against another one.  I don't see any "I looked around but I only see one dimensional sorts not two.", there is no dimensionality issue here.</p>
]]></description><link>https://forum.qt.io/post/569892</link><guid isPermaLink="true">https://forum.qt.io/post/569892</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 01 Jan 2020 08:55:28 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Tue, 31 Dec 2019 21:58:32 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">What data are these data ?</p>
]]></description><link>https://forum.qt.io/post/569874</link><guid isPermaLink="true">https://forum.qt.io/post/569874</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 31 Dec 2019 21:58:32 GMT</pubDate></item><item><title><![CDATA[Reply to How to sort QList&lt;QList&lt;QVariant&gt;&gt; on Tue, 31 Dec 2019 19:39:10 GMT]]></title><description><![CDATA[<p dir="auto">Also I tried copying the model data back to the data source after the sort, but that process takes way too long especially if there are hundreds of thousands of rows so that is not a viable option.</p>
]]></description><link>https://forum.qt.io/post/569855</link><guid isPermaLink="true">https://forum.qt.io/post/569855</guid><dc:creator><![CDATA[leinad]]></dc:creator><pubDate>Tue, 31 Dec 2019 19:39:10 GMT</pubDate></item></channel></rss>