[Interest] qSort replacement

Ahmad Samir a.samirh78 at gmail.com
Wed Jul 21 18:13:59 CEST 2021


On 21/07/2021 18:05, Joshua Grauman wrote:
> Hi all,
> 
> I see that qSort is deprecated in newer versions of Qt. I can also see
> that std::sort is intended to replace it. But I can't figure out how to
> use std::sort to sort a QList with a custom compare function. With qSort
> you could supply your own comparison function to sort however you wanted.
> I don't see how to do this with std::sort. Do you have to subclass and
> provide a operator< overload? I'd rather just define a comparison
> function, is this possible?
> 
> Thanks,
> 
> Josh
> 
> bool variantLessThan(const QVariant &v1, const QVariant &v2)
> {
>      return v1.toString() < v2.toString();
> }
> 
> qSort(fieldsList.begin(), fieldsList.end(), variantLessThan);

In the above example, just replace qSort with std::sort. It should work.

See:
https://en.cppreference.com/w/cpp/algorithm/sort

Another usage example:
https://invent.kde.org/utilities/konsole/-/blob/master/src/profile/ProfileManager.cpp#L186


-- 
Ahmad Samir


More information about the Interest mailing list