On this page

QCollator Class

The QCollator class compares strings according to a localized collation algorithm. More...

Header: #include <QCollator>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

Note: All functions in this class are reentrant.

QCollator Comparisons

CategoryComparable Types
equalityQCollator

Public Types

(since 6.13) enum class CollationOption { CaseInsensitive, IgnorePunctuation, NumericSort, DiacriticInsensitive }
flags CollationOptions

Public Functions

QCollator()
QCollator(const QLocale &locale)
QCollator(const QCollator &other)
QCollator(QCollator &&other)
~QCollator()
Qt::CaseSensitivity caseSensitivity() const
int compare(QStringView s1, QStringView s2) const
int compare(const QString &s1, const QString &s2) const
int compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
bool ignorePunctuation() const
QLocale locale() const
bool numericMode() const
(since 6.13) QCollator::CollationOptions options() const
void setCaseSensitivity(Qt::CaseSensitivity cs)
void setIgnorePunctuation(bool on)
void setLocale(const QLocale &locale)
void setNumericMode(bool on)
(since 6.13) void setOptions(QCollator::CollationOptions options)
QCollatorSortKey sortKey(const QString &string) const
void swap(QCollator &other)
bool operator()(QStringView s1, QStringView s2) const
bool operator()(const QString &s1, const QString &s2) const
QCollator &operator=(QCollator &&other)
QCollator &operator=(const QCollator &other)

Static Public Members

(since 6.3) int defaultCompare(QStringView s1, QStringView s2)
(since 6.3) QCollatorSortKey defaultSortKey(QStringView key)
(since 6.12) bool operator!=(const QCollator &lhs, const QCollator &rhs)
(since 6.12) bool operator==(const QCollator &lhs, const QCollator &rhs)

Detailed Description

QCollator is initialized with a QLocale. It can then be used to compare and sort strings by using the ordering appropriate for that locale.

A QCollator object can be used together with template-based sorting algorithms, such as std::sort(), to sort a list with QString entries.

QStringList sortedStrings(QStringList seq)
{
    QCollator order;
    std::sort(seq.begin(), seq.end(), order);
    return seq;
}

In addition to the locale, several optional flags can be set that influence the result of the collation.

POSIX fallback implementation

On Unix systems, Qt is normally compiled to use ICU (except for macOS, where Qt defaults to using an equivalent Apple API). However, if ICU was not available at compile time or explicitly disabled, Qt will use a fallback backend that uses the POSIX API only. This backend has several limitations:

The use of any of the unsupported options will cause a warning to be printed to the application's output.

Member Type Documentation

[since 6.13] enum class QCollator::CollationOption
flags QCollator::CollationOptions

Options that control how strings are compared by QCollator.

The macOS, Windows, and ICU backends support all options, with variations where indicated. On non-macOS Unix (including Linux), if ICU is not available a fallback (POSIX) backend is used, which supports none of these options.

ConstantValueDescription
QCollator::CollationOption::CaseInsensitive0x1Ignore case differences when comparing strings.
QCollator::CollationOption::IgnorePunctuation0x2Ignore punctuation and symbols when comparing strings.
QCollator::CollationOption::NumericSort0x4Sort strings containing numbers by their numeric value, so that for example "file10" sorts after "file9" instead of between "file1" and "file2".
QCollator::CollationOption::DiacriticInsensitive0x8Ignore diacritical marks when comparing strings, so that for example "e" and "é" compare as equal. Depending on the locale, this may also include ligature folding such as æ == ae. See DiacriticInsensitive behavior details below.
DiacriticInsensitive behavior details

This option is primarily intended for search and matching, where the user may not type diacritics. For example, typing "resume" in a search field and expecting to find "résumé".

The exact behavior depends on the locale and platform backend:

  • Characters that are independent letters in a given alphabet are not treated as diacritic variants. For example, in Swedish "å", "ä" and "ö" are separate letters that sort after "z", so they do not compare equal to "a" or "o", even with this option set. Their position in the alphabet is also preserved. The same characters may behave differently in another locale: in German, "ö" and "ä" are treated as variants of "o" and "a" and do compare equal to them with this option set.
  • Collation folding for locale-specific equivalences (such as å == aa in Norwegian) is locale-dependent and works consistently across backends. Note that this maps "å" to the digraph "aa", not to a single "a"; "å" is an independent letter and never folds to "a".
  • Ligature folding (such as ß == ss and æ == ae) is applied on Windows and ICU, where the locale defines it; for example "æ" folds to "ae" in English but is an independent letter in Norwegian and does not fold. This folding is not supported by the macOS backend. On Windows, some ligature folding is applied implicitly, even without this option set.

This enum was introduced in Qt 6.13.

The CollationOptions type is a typedef for QFlags<CollationOption>. It stores an OR combination of CollationOption values.

See also setOptions() and options().

Member Function Documentation

QCollator::QCollator()

Constructs a QCollator using the default locale's collation locale.

The system locale, when used as default locale, may have a collation locale other than itself (e.g. on Unix, if LC_COLLATE is set differently to LANG in the environment). All other locales are their own collation locales.

See also setLocale(), QLocale::collation(), and QLocale::setDefault().

[explicit] QCollator::QCollator(const QLocale &locale)

Constructs a QCollator using the given locale.

See also setLocale().

QCollator::QCollator(const QCollator &other)

Creates a copy of other.

[noexcept] QCollator::QCollator(QCollator &&other)

Move constructor. Moves from other into this collator.

Note: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

[noexcept] QCollator::~QCollator()

Destroys this collator.

Qt::CaseSensitivity QCollator::caseSensitivity() const

Returns case sensitivity of the collator.

This defaults to case-sensitive until set.

Note: In the C locale, when case-sensitive, all lower-case letters sort after all upper-case letters, where most locales sort each lower-case letter either immediately before or immediately after its upper-case partner. Thus "Zap" sorts before "ape" in the C locale but after in most others.

See also setCaseSensitivity().

int QCollator::compare(QStringView s1, QStringView s2) const

Compares s1 with s2.

Returns a negative integer if s1 is less than s2, a positive integer if it is greater than s2, and zero if they are equal.

int QCollator::compare(const QString &s1, const QString &s2) const

This is an overloaded function.

int QCollator::compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const

Compares s1 with s2. len1 and len2 specify the lengths of the QChar arrays pointed to by s1 and s2.

Returns a negative integer if s1 is less than s2, a positive integer if it is greater than s2, and zero if they are equal.

Note: In Qt versions prior to 6.4, the length arguments were of type int, not qsizetype.

This is an overloaded function.

[static, since 6.3] int QCollator::defaultCompare(QStringView s1, QStringView s2)

Compares the strings s1 and s2, returning their sorting order. This function performs the same operation as compare() on a default-constructed QCollator object.

This function was introduced in Qt 6.3.

See also compare() and defaultSortKey().

[static, since 6.3] QCollatorSortKey QCollator::defaultSortKey(QStringView key)

Returns the sort key for the string key. This function performs the same operation as sortKey() on a default-constructed QCollator object.

This function was introduced in Qt 6.3.

See also sortKey() and defaultCompare().

bool QCollator::ignorePunctuation() const

Returns whether punctuation and symbols are ignored when collating.

When true, strings are compared as if all punctuation and symbols were removed from each string.

See also setIgnorePunctuation().

QLocale QCollator::locale() const

Returns the locale of the collator.

Unless supplied to the constructor or by calling setLocale(), the system's default collation locale is used.

See also setLocale() and QLocale::collation().

bool QCollator::numericMode() const

Returns true if numeric sorting is enabled, false otherwise.

When true, numerals are recognized as numbers and sorted in arithmetic order; for example, 100 sortes after 99. When false, numbers are sorted in lexical order, so that 100 sorts before 99 (because 1 is before 9). By default, this option is disabled.

See also setNumericMode().

[since 6.13] QCollator::CollationOptions QCollator::options() const

Returns the collation options currently set on the collator.

This function was introduced in Qt 6.13.

See also setOptions() and CollationOption.

void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)

Sets the case-sensitivity of the collator to cs.

See also caseSensitivity().

void QCollator::setIgnorePunctuation(bool on)

Ignores punctuation and symbols if on is true, attends to them if false.

See also ignorePunctuation().

void QCollator::setLocale(const QLocale &locale)

Sets the locale of the collator to locale.

See also locale().

void QCollator::setNumericMode(bool on)

Enables numeric sorting mode when on is true.

See also numericMode().

[since 6.13] void QCollator::setOptions(QCollator::CollationOptions options)

Sets the collation options to options. This allows configuring multiple collation settings at once.

Note: In the C locale, the collation options have no effect. Use a specific locale to enable locale-aware collation.

This function was introduced in Qt 6.13.

See also options() and CollationOption.

QCollatorSortKey QCollator::sortKey(const QString &string) const

Returns a sortKey for string.

Creating the sort key is usually somewhat slower, than using the compare() methods directly. But if the string is compared repeatedly (e.g. when sorting a whole list of strings), it's usually faster to create the sort keys for each string and then sort using the keys.

Note: Not supported with the C (a.k.a. POSIX) locale on Darwin.

[noexcept] void QCollator::swap(QCollator &other)

Swaps this collator with other. This operation is very fast and never fails.

bool QCollator::operator()(QStringView s1, QStringView s2) const

A QCollator can be used as the comparison function of a sorting algorithm. It returns true if s1 sorts before s2, otherwise false.

See also compare().

bool QCollator::operator()(const QString &s1, const QString &s2) const

This is an overloaded function.

[noexcept] QCollator &QCollator::operator=(QCollator &&other)

Move-assigns other to this QCollator instance.

Note: The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.

QCollator &QCollator::operator=(const QCollator &other)

Assigns other to this collator.

Related Non-Members

[noexcept, since 6.12] bool operator!=(const QCollator &lhs, const QCollator &rhs)

[noexcept, since 6.12] bool operator==(const QCollator &lhs, const QCollator &rhs)

Returns true if lhs and rhs use the same locale and collation options, otherwise returns false.

These functions were introduced in Qt 6.12.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.