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
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to