View Full Version : Best way to sort one user-defined struct
stella1016
2nd May 2012, 16:57
I have a user-defined data type, say "people". It has parameter: id (unique), number_of_books, age, weight. The data set is built.
Now I want to have the ordered id with following criterias:
1. number_of_books
2. age
3. weight
which means, the order mainly depends on number_of_books, then age, then weight.
How could I implement this by using QT?
I am thinking about qSort(), but it has only one key to sort, right?
Thanks a lot~
qSort use the less than operator so you must implement one for your struct and the use qSort
stella1016
2nd May 2012, 17:42
Thanks. I tried, but there is error. Do you know the problem? Thanks again.
qSort(before_sort.begin(), before_sort.end(), qGreater<people>);
bool MyClass::qGreater(people p1, people p2)
{
return true;
}
qSort use the less than operator so you must implement one for your struct and the use qSort
yeye_olive
2nd May 2012, 18:15
I could not find a clear confirmation of this in the docs, but it seems to me that qGreater<T>() just calls operator>() for operands of type T. If you want to use qGreater() you have to implement the > operator for your class.
Alternatively you can:
- define the operator < for your class and call qSort(begin, end);
- pick any name for your comparison function, like bool myComparisonFunction(const T &lhs, const T &rhs) and call qSort(begin, end, myComparisonFunction).
In any case all you have to do is implement in your comparison function the lexicographical comparison you described.
exactly in your people struct just put:
bool operator <(const T& b) const
{
// put you code here
}
and then use :
QList<people> lst_people;
qSort(lst_people);
Lykurg
2nd May 2012, 21:17
or without modifying the struct use
qSort(before_sort.begin(), before_sort.end(), MyClass::qGreater);
bool MyClass::qGreater(const people& p1, const people& p2)
{
return true;
}
amleto
2nd May 2012, 23:20
are you sure that will work with member function as comparer?
e:
just tested, and it wont work.
needs to be a free function. or maybe a static.
yeye_olive
3rd May 2012, 00:46
Right, it should be a standalone function or a static method.
stella1016
3rd May 2012, 09:37
Thanks a lot for all the replies!
The following code works. That's right, the compare method has to be static, should be not as a member function of this class.
bool qGreater(people p1, people p2)
{
return true;
}
QList<people> MyClass::sorting_people()
{
...
qSort(before_sort.begin(), before_sort.end(), qGreater);
...
}
Lykurg
3rd May 2012, 19:27
Also member functions can be static! So instead polluting the global namespace I would at least create a namespace for comparing function. And as for your case if you have a function MyClass::sorting_people, I would probably go for a static member function called MyClass::sorting_people_helper or something like that.
Pandimensional
29th June 2012, 11:59
Hi
I cant quite understand, I have hte following:
QList<QWidget *> tempList = tempLegend->findChildren<QWidget *>();
qSort(tempList.begin(), tempList.end(), qGreater);
but this is in a PlotWindow::PlotWindow constructor. The following error is generated:
missing template arguements before ) token.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.