PDA

View Full Version : how to sort a list by one of its items ?! [solved]



gbmtoday
2nd January 2011, 19:17
Hey Guys

I got this :


struct wordItem {
QString word;
int count;
};

QList <wordItem> list;


now I want to know what is the fastest way to sort the list by wordItem.count ?!
Is there any function in Qt or C++ to do this for me ?!

Thank you for your time:)

Zlatomir
2nd January 2011, 19:29
You can use qSort (http://doc.qt.nokia.com/4.7/qtalgorithms.html#qSort)

gbmtoday
2nd January 2011, 19:44
Here's my code




Qlist <WordItem> list;

qStableSort(list.begin(),list.end(),compareWordIte m);

bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
{
return w1.count > w2.count;
}


but I get this :

error: no matching function for call to ‘qStableSort(QList<WordItem>::iterator, QList<WordItem>::iterator, <unresolved overloaded function type>)’

Zlatomir
2nd January 2011, 19:53
Did you #include <QtAlgorithms>?

gbmtoday
2nd January 2011, 19:54
Did you #include <QtAlgorithms>?

I included it but it did not make any differnce .:(

tbscope
2nd January 2011, 19:58
Did you create a function prototype of the sort function before using it?

Zlatomir
2nd January 2011, 19:59
Also the compare function/functor should be declared before calling qSort


bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
{
return w1.count > w2.count;
}
qStableSort(list.begin(),list.end(),compareWordIte m);


LE: you declare/define compareWordItem outside of any function (including main)

gbmtoday
2nd January 2011, 20:02
Did you create a function prototype of the sort function before using it?

what do you mean ?:confused:

I just did what I said above !:confused:


Also the compare function/functor should be declared before calling qSort


bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
{
return w1.count > w2.count;
}
qStableSort(list.begin(),list.end(),compareWordIte m);


LE: you declare/define compareWordItem outside of any function (including main)

I already did that .

tbscope
2nd January 2011, 20:02
As Zlatomir says

This is your clue:

<unresolved overloaded function type>

gbmtoday
2nd January 2011, 20:07
As Zlatomir says

This is your clue:

<unresolved overloaded function type>

if you mean defining it in my header file , yeah I defined it in my .h file

Zlatomir
2nd January 2011, 20:10
No, you should define that in the .cpp file, don't define functions in the .h file, you might get multiple-definitions errors if you include the header in many .cpp files.
In the header files you should put only declarations, not definitions...

I made a simple example, the code works, you should play with where you put the declarations/definitions, this is the little test i made:


#include <QtCore>
#include <iostream>

struct WordItem {
WordItem(int i) : count(i) {}
int count;
};

bool compareWordItem ( const WordItem &w1 , const WordItem &w2 )
{
return w1.count > w2.count;
}

int main(int argc, char *argv[])
{
QList<WordItem> list;

list.append(10);
list.append(11);
list.append(1);

qStableSort(list.begin(),list.end(),compareWordIte m);

for(QList<WordItem>::iterator i = list.begin(); i!= list.end(); ++i)
qDebug() << (*i).count;

std::cin.get();
return 0;
}

gbmtoday
2nd January 2011, 20:30
No I meant I declared it in my .h and defined it .cpp ( sorry ! bad English ):)

I don't get it , my code looks exactly as yours except that mine is in a class .

tbscope
2nd January 2011, 20:33
except that mine is in a class .

Don't forget the namespace!

gbmtoday
2nd January 2011, 20:37
finally I figured what is the problem .

I should have defined it as a static function.
in .h :

static bool compareWordItem ( const WordItem &w1 , const WordItem &w2 );


thanks guys