QCollator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(add example)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{background:#009900}. ||''. ICU |''. POSIX |''. Mac OS X |''. Windows |
== Available features by platform ==
|casePreference| Y | N | Y | Y |
 
|numericMode | Y | N | Y | N¹ |
{| class="wikitable"
|ignorePunctuation | Y | N | Y | Y |
|
|compare | Y | Y | Y | Y |
! ICU  
|sortKey | Y | Y | Y| Y |
! POSIX  
! Mac OS X  
! Windows  
|-
|casePreference
| Y  
| N  
| Y  
| Y  
|-
|numericMode  
| Y  
| N  
| Y  
| N¹  
|-
|ignorePunctuation  
| Y  
| N  
| Y  
| Y  
|-
|compare  
| Y  
| Y  
| Y  
| Y  
|-
|sortKey  
| Y  
| Y  
| Y
| Y  
|}


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