QCollator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(add example)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{| class="infotable line"
== Available features by platform ==
|- style="background: #009900"
 
{| class="wikitable"
|
|
! <span class="caps">ICU</span>
! ICU  
! <span class="caps">POSIX</span>
! POSIX  
! Mac OS X
! Mac OS X  
! Windows
! Windows  
|-
|-
| casePreference
|casePreference
| Y
| Y  
| N
| N  
| Y
| Y  
| Y
| Y  
|-
|-
| numericMode
|numericMode  
| Y
| Y  
| N
| N  
| Y
| Y  
| N¹
| N¹  
|-
|-
| ignorePunctuation
|ignorePunctuation  
| Y
| Y  
| N
| N  
| Y
| Y  
| Y
| Y  
|-
|-
| compare
|compare  
| Y
| Y  
| Y
| Y  
| Y
| Y  
| Y
| Y  
|-
|-
| sortKey
|sortKey  
| Y
| Y  
| Y
| Y  
| 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")