[Interest] qSort replacement

John Weeks john at wavemetrics.com
Wed Jul 21 18:42:26 CEST 2021


Lambdas as predicates are supported in C++11, too.

> On Jul 21, 2021, at 9:22 AM, Rainer Wiesenfarth <Rainer_Wiesenfarth at trimble.com> wrote:
> 
> On Wed, Jul 21, 2021 at 6:07 PM Joshua Grauman <jnfo-d at grauman.com> wrote:
> [...] I can't figure out how to 
> use std::sort to sort a QList with a custom compare function. [...]
> bool variantLessThan(const QVariant &v1, const QVariant &v2)
> {
>     return v1.toString() < v2.toString();
> }
> 
> qSort(fieldsList.begin(), fieldsList.end(), variantLessThan);
> [...]
> 
> If you are using C++17 you may also use lambdas:
> 
> std::sort(fieldsList.begin(), fieldsList.end(), [](const QVariant &v1, const QVariant &v2) {
>     return v1.toString() < v2.toString();
> });
> 
> (untested)
> 
> Cheers, Rainer
> 
> -- 
> Software Engineer | Trimble Imaging Division
> Rotebühlstraße 81 | 70178 Stuttgart | Germany
> Office +49 711 22881 0 | Fax +49 711 22881 11
> http://www.trimble.com/imaging/ | http://www.inpho.de/
> 
> Trimble Germany GmbH, Am Prime Parc 11, 65479 Raunheim
> Eingetragen beim Amtsgericht Darmstadt unter HRB 83893,
> Geschäftsführer: Rob Reeder, Jürgen Kesper
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest

-John Weeks



More information about the Interest mailing list