PDA

View Full Version : QSort



skumar7777
2nd December 2008, 02:42
Hi,

I have a problem with qSort, illustrated as follows:


#include <qhash.h>

struct TDATA {
QString emp_name;
float salary;
};

QHash < uint, TDATA> emps;

bool salaryLessThan(const TDATA &e1, const TDATA &e2) {
return (e1.salary < e2.salary);
}

int main(int argc, char *argv[])
{

qStableSort(emps.begin(), emps.end(), salaryLessThan);
return 1;
}

Basically, I want to sort based on the values in the Hash.

Compiling this gives a long error message as attached:

Can anyone please help me out?

Thanks.

pgorszkowski
2nd December 2008, 05:13
You cannot use qStableSort to sort qHash because qHash does not have Random Access Iterators. If you want sorting by qStableSort you have to use qVector or qList.

http://doc.trolltech.com/4.3/qtalgorithms.html#qStableSort

http://doc.trolltech.com/4.3/qtalgorithms.html#random-access-iterators

skumar7777
2nd December 2008, 05:23
Hi,

Thanks for responding.

Please take a look at http://doc.trolltech.com/4.4/qtalgorithms.html.

It clearly states

You can use these algorithms with any container class that provides STL-style iterators, including Qt's QList, QLinkedList, QVector, QMap, and QHash classes.

I think it is a problem with my function definition. I have looked at similiar threads but could not figure out what was required since my function is global.

Thanks again.

spirit
2nd December 2008, 05:51
read this and maybe you understand why items in QHash can't be sorted Qt containers (http://doc.trolltech.com/qq/qq19-containers.html).

skumar7777
2nd December 2008, 06:18
Got it!
Thanks to spirit & pgorszkowski.