Best way to sort one user-defined struct
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~
Re: Best way to sort one user-defined struct
qSort use the less than operator so you must implement one for your struct and the use qSort
Re: Best way to sort one user-defined struct
Thanks. I tried, but there is error. Do you know the problem? Thanks again.
Code:
qSort(before_sort.begin(), before_sort.end(), qGreater<people>);
bool MyClass::qGreater(people p1, people p2)
{
return true;
}
Quote:
Originally Posted by
Le_B
qSort use the less than operator so you must implement one for your struct and the use qSort
Re: Best way to sort one user-defined struct
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.
Re: Best way to sort one user-defined struct
exactly in your people struct just put:
Code:
bool operator <(const T& b) const
{
// put you code here
}
and then use :
Code:
QList<people> lst_people;
qSort(lst_people);
Re: Best way to sort one user-defined struct
or without modifying the struct use
Code:
qSort(before_sort.begin(), before_sort.end(), MyClass::qGreater);
bool MyClass::qGreater(const people& p1, const people& p2)
{
return true;
}
Re: Best way to sort one user-defined struct
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.
Re: Best way to sort one user-defined struct
Right, it should be a standalone function or a static method.
Re: Best way to sort one user-defined struct
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.
Code:
bool qGreater(people p1, people p2)
{
return true;
}
QList<people> MyClass::sorting_people()
{
...
qSort(before_sort.begin(), before_sort.end(), qGreater);
...
}
Re: Best way to sort one user-defined struct
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.
Re: Best way to sort one user-defined struct
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.