How do I implement custom sorting of a QJsonArray
based on one of its children?
I have QJsonArray toys
based on this JSON:
"toys": [
{
"type": "teddy",
"name": "Thomas",
"size": 24
},
{
"type": "giraffe",
"name": "Jenny",
"size": 28
},
{
"type": "alligator",
"name": "Alex",
"size": 12
}
]
I want to sort this alphabetically by "name"
.
I tried this:
std::sort(toys.begin(), toys.end(), [](const QJsonObject &v1, const QJsonObject &v2) {
return v1["name"].toString() < v2["name"].toString();
});
but this throws lots of errors.
There are a couple of things that need fixing. First, here's my solution and below some explanations:
inline void swap(QJsonValueRef v1, QJsonValueRef v2)
{
QJsonValue temp(v1);
v1 = QJsonValue(v2);
v2 = temp;
}
std::sort(toys.begin(), toys.end(), [](const QJsonValue &v1, const QJsonValue &v2) {
return v1.toObject()["name"].toString() < v2.toObject()["name"].toString();
});
One of the errors you had was:
no matching function for call to object of type '(lambda at xxxxxxxx)'
if (__comp(*--__last, *__first))
^~~~~~
...
candidate function not viable: no known conversion from 'QJsonValueRef' to 'const QJsonObject' for 1st argument
std::sort(toys.begin(), toys.end(), [](const QJsonObject &v1, const QJsonObject &v2) {
^
...
The iterator does not know that your array elements are of type QJsonObject
. Instead it sees them as the generic QJsonValue
type. There's no automatic conversion to QJsonObject
, so it throws an error with your lambda function.
Replace const QJsonObject &
with const QJsonValue &
for both lambda arguments. Then handle the conversion to QJsonObject
type explicitly in the function body: v1.toObject()...
instead of v1...
.
One of the errors you had was:
no matching function for call to 'swap'
swap(*__first, *__last);
^~~~
As discussed in Qt bug report QTBUG-44944, Qt does not provide an implementation for swapping over the two QJsonValue
elements in the array. Thanks to the bug reporter Keith Gardner, we can include our own swap function. As suggested in the report, you may want to put that as an inline function in a global header file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With