QCollator

From Qt Wiki
Jump to navigation Jump to search

Available features by platform

ICU POSIX Mac OS X Windows
casePreference Y N Y Y
numericMode Y N Y
ignorePunctuation Y N Y Y
compare Y Y Y Y
sortKey Y Y Y Y

¹ Only available since Win7

Example: Sorting a QStringList using QCollator

#include <QStringList>
#include <QCollator>
#include <QDebug>

int main( int argc, char *argv[] )
{
    QStringList myStringList = (QStringList() << "abc123" << "ABc99" << "1234abcd");

    QCollator sorter;
    sorter.setNumericMode( true ); // Default is false
    sorter.setCaseSensitivity( Qt::CaseInsensitive ); // Default is case sensitive

    std::sort( myStringList.begin(), myStringList.end(), [&]( const QString& a, const QString& b ) {
        return sorter.compare( a, b ) < 0;
    } );

    qDebug() << myStringList;
}

The code above should output the following:

("1234abcd", "ABc99", "abc123")