QCollator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(table)
(add example)
 
Line 1: Line 1:
 
== Available features by platform ==


{| class="wikitable"
{| class="wikitable"
Line 40: Line 40:


¹ Only available since Win7
¹ Only available since Win7
== Example: Sorting a QStringList using QCollator ==
<code>
#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;
}
</code>
The code above should output the following:
:<tt>("1234abcd", "ABc99", "abc123")</tt>

Latest revision as of 15:57, 1 July 2016

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")