Quick sort QStringList : QStringList « Qt « C++






Quick sort QStringList

  

#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.Adding QStringList to QListWidget
2.A simple model that uses a QStringList as its data source
3.Complete demo for QStringList
4.Split QStringList
5.QStringList demo
6.STL-Style Iterators
7.Search in a list of fruit names first for the word Pear and then for Orange
8.using QStringList, append to and output and join