Quick sort QList : QList « Qt « C++






Quick sort QList

  
#include <QList>
#include <QtAlgorithms>   // for qSort()
#include <QStringList>
#include <QDebug>

class CaseIgnoreString : public QString {
public:
    CaseIgnoreString(const QString& other = QString()) : QString(other) {}

    bool operator<(const QString & other) const {
        return toLower() < other.toLower();
    }
    bool operator==(const QString& other) const {
        return toLower() == other.toLower();
    }
};

int main() {
    CaseIgnoreString s1("A"), s2("b"), s3 ("C"), s4("d"), s5 ("D");
    QList<CaseIgnoreString> namelist;

    namelist << s5 << s1 << s3 << s4 << s2;
 
    qSort(namelist.begin(), namelist.end());
    int i=0;
    foreach (QString stritr, namelist) {
        qDebug() << QString("namelist[%1] = %2").arg(i++).arg(stritr) ;
    }

    QStringList strlist;
    strlist << s5 << s1 << s3 << s4 << s2; 

    qSort(strlist.begin(), strlist.end());
    qDebug() << "StringList sorted: " + strlist.join(", ") << endl;
    return 0;
}

   
    
  








Related examples in the same category

1.Add int to QList
2.Append and insert into QList
3.Foreach loop with QList
4.Sorting QList
5.Find next element in a QList with QMutableListIterator
6.QList of QString
7.Delete all elements in a QList
8.Filling Data Structures
9.QList of int