QCollator Class

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

Header: #include <QCollator>
qmake: QT += core
Since: Qt 5.2

Note: All functions in this class are reentrant.

Public Functions

QCollator()
QCollator(const QLocale &locale)
QCollator(const QCollator &other)
~QCollator()
Qt::CaseSensitivity caseSensitivity() const
int compare(QStringView s1, QStringView s2) const
int compare(const QStringRef &s1, const QStringRef &s2) const
int compare(const QChar *s1, int len1, const QChar *s2, int len2) const
int compare(const QString &s1, const QString &s2) const
bool ignorePunctuation() const
QLocale locale() const
bool numericMode() const
void setCaseSensitivity(Qt::CaseSensitivity sensitivity)
void setIgnorePunctuation(bool on)
void setNumericMode(bool on)
QCollatorSortKey sortKey(const QString &string) const
QCollator &operator=(const QCollator &other)

Detailed Description

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

QCollator is initialized with a QLocale and an optional collation strategy. It tries to initialize the collator with the specified values. The collator can then be used to compare and sort strings in a locale dependent fashion.

A QCollator object can be used together with template based sorting algorithms such as std::sort to sort a list of QStrings.

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

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.

This function was introduced in Qt 5.13.

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

QCollator::QCollator(const QLocale &locale)

Constructs a QCollator from locale.

See also setLocale().

QCollator::QCollator(const QCollator &other)

Creates a copy of other.

QCollator::~QCollator()

Destroys the collator.

Qt::CaseSensitivity QCollator::caseSensitivity() const

Returns case sensitivity of the collator.

See also setCaseSensitivity().

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

Compares s1 with s2.

Returns an integer less than, equal to, or greater than zero depending on whether s1 sorts before, with or after s2.

This function was introduced in Qt 5.13.

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

This is an overloaded function.

Compares s1 with s2.

Returns an integer less than, equal to, or greater than zero depending on whether s1 sorts before, with or after s2.

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

This is an overloaded function.

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

Returns an integer less than, equal to, or greater than zero depending on whether s1 sorts before, with or after s2.

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

This is an overloaded function.

Compares s1 with s2.

Returns an integer less than, equal to, or greater than zero depending on whether s1 sorts before, with or after s2.

bool QCollator::ignorePunctuation() const

Returns true if punctuation characters and symbols are ignored when determining sort order.

See also setIgnorePunctuation().

QLocale QCollator::locale() const

Returns the locale of the collator.

bool QCollator::numericMode() const

Returns true if numeric sorting is enabled, false otherwise.

See also setNumericMode().

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

Sets the case sensitivity of the collator.

See also caseSensitivity().

void QCollator::setIgnorePunctuation(bool on)

If on is set to true, punctuation characters and symbols are ignored when determining sort order.

The default is locale dependent.

Note: This method is not currently supported if Qt is configured to not use ICU on Linux.

See also ignorePunctuation().

void QCollator::setNumericMode(bool on)

Enables numeric sorting mode when on is set to true.

This will enable proper sorting of numeric digits, so that e.g. 100 sorts after 99.

By default this mode is off.

See also numericMode().

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.

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

Assigns other to this collator.