PDA

View Full Version : Sorting a StringList



Jimmy2775
10th February 2006, 21:14
Hello

I am trying to write the sort function for a QStringList object. I know that there is already a built-in function, but it is not sufficient for what I need because it only sorts ascending.

Here is the code I am using:


QStringList MyClass::sort(QStringList stringList, Qt::SortOrder order)
{
if ( order == Qt::AscendingOrder )
{
qSort(stringList.begin(), stringList.end(), caseInsensitiveLessThan);
}
else
{
qSort(stringList.begin(), stringList.end(), caseInsensitiveGreaterThan);
}
return stringList;
}

bool MyClass::caseInsensitiveLessThan(const QString &s1, const QString &s2)
{
return s1.toLower() < s2.toLower();
}

bool MyClass::caseInsensitiveGreaterThan(const QString &s1, const QString &s2)
{
return s1.toLower() > s2.toLower();
}


This is based on examples provided in the QtAlgorithms documentation. The problem I am having is that when I try to compile I get a compile-time error:

term does not evaluate to a function taking 2 arguments
c:\Qt\4.1.0\include\QtCore\../../src\corelib\tools\qalgorithms.h(141) : see reference to function template instantiation 'void QAlgorithmsPrivate::qSortHelper<BiIterator,T,LessThan>(BiIterator,BiIterator,const T &,LessThan)' being compiled
with
[
BiIterator=QList<QString>::iterator,
T=QString,
LessThan=bool (__thiscall MyClass::* )(const QString &,const QString &)
]
myclass.cpp(90) : see reference to function template instantiation 'void qSort<QList<T>::iterator,bool(__thiscall MyClass::* )(const QString &,const QString &)>(BiIterator,BiIterator,LessThan)' being compiled
with
[
T=QString,
BiIterator=QList<QString>::iterator,
LessThan=bool (__thiscall MyClass::* )(const QString &,const QString &)
]

I am new to Qt and my C++ is very rusty. I would appreciate some advice with regards to debugging this problem and/or interpreting this error msg.

Thanks in advance,

Jimmy

wysota
10th February 2006, 21:19
qSort(stringlist.begin(), stringlist.end(), qGreater<QString>());

This might be a bit more simple.

Jimmy2775
10th February 2006, 21:26
That is simpler and does solve the error - thank you wysota.

However, I would still like to know what the problem was with my origional code that would cause it not to compile so I can understand it better.

jacek
10th February 2006, 21:59
However, I would still like to know what the problem was with my origional code that would cause it not to compile so I can understand it better.
You were trying to use a pointer to a method instead of a pointer to a standalone function.

This should work:
bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
{
return s1.toLower() < s2.toLower();
}

bool caseInsensitiveGreaterThan(const QString &s1, const QString &s2)
{
return s1.toLower() > s2.toLower();
}

QStringList MyClass::sort(QStringList stringList, Qt::SortOrder order)
{
if ( order == Qt::AscendingOrder )
{
qSort(stringList.begin(), stringList.end(), caseInsensitiveLessThan);
}
else
{
qSort(stringList.begin(), stringList.end(), caseInsensitiveGreaterThan);
}
return stringList;
}

Jimmy2775
10th February 2006, 22:11
OK - I understand. Thank you.