6.0.0-rc4Source

QCollator Class

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

Header#include <QCollator>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since5.2

List of all members, including inherited members

Public Functions

QCollator()
QCollator(const QLocale &locale)
QCollator(const QCollator &other)
QCollator(QCollator &&other)
~QCollator()
QCollator &operator=(const QCollator &other)
QCollator &operator=(QCollator &&other)
voidswap(QCollator &other)
voidsetLocale(const QLocale &locale)
QLocalelocale() const
Qt::CaseSensitivitycaseSensitivity() const
voidsetCaseSensitivity(Qt::CaseSensitivity cs)
voidsetNumericMode(bool on)
boolnumericMode() const
voidsetIgnorePunctuation(bool on)
boolignorePunctuation() const
intcompare(QStringView s1, QStringView s2) const
intcompare(const QString &s1, const QString &s2) const
intcompare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
booloperator()(QStringView s1, QStringView s2) const
booloperator()(const QString &s1, const QString &s2) const
QCollatorSortKeysortKey(const QString &string) const

Static Public Members

Detailed Description

A collator orders strings as people of a locale expect, unlike QString::compare(), which compares code units: letters are compared before accents, accents before case, and with numericMode() digits are compared by value.

QCollator collator;
collator.setNumericMode(true);
std::sort(files.begin(), files.end(), collator);   // "file2" before "file10"

See also QCollatorSortKey and QLocale.

Differences from Qt

Nt implements its own simplified collation instead of using ICU: letters are compared by their base letter (with Latin accents removed), then by accent, then by case (lowercase first). Letters of other scripts sort after Latin by code point. Tailorings exist for the Nordic languages (å, ä, æ, ö, ø after z), Spanish (ñ), Polish and Czech; other languages use the default order. Expansions are limited to ß, æ, œ and similar letters, and there are no contractions such as Czech "ch".

Member Function Documentation

QCollator::QCollator()

Constructs a collator for the default locale.

See also QLocale::setDefault().

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

Constructs a collator for locale.

QCollator::QCollator(const QCollator &other)

Constructs a copy of other.

QCollator::QCollator(QCollator &&other)

Move-constructs a collator from other.

QCollator::~QCollator()

Destroys the collator.

Qt::CaseSensitivity QCollator::caseSensitivity() const

Returns the case sensitivity of the collator. The default is Qt::CaseSensitive.

See also setCaseSensitivity().

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

Compares s1 with s2 and returns a negative number, zero or a positive number if s1 sorts before, equal to or after s2 in the collator's locale.

See also sortKey().

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

This is an overloaded function.

Compares s1 with s2 and returns a negative number, zero or a positive number if s1 sorts before, equal to or after s2.

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

This is an overloaded function.

Compares the len1 characters at s1 with the len2 characters at s2.

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

Compares s1 with s2 using a collator for the default locale with the default settings.

This function was introduced in 6.3.

[static] QCollatorSortKey QCollator::defaultSortKey(QStringView key)

Returns the sort key of key for a collator with the default locale and settings.

This function was introduced in 6.3.

bool QCollator::ignorePunctuation() const

Returns true if punctuation, symbols and white space are ignored; otherwise returns false.

See also setIgnorePunctuation().

QLocale QCollator::locale() const

Returns the locale of the collator.

See also setLocale().

bool QCollator::numericMode() const

Returns true if numeric mode is on; otherwise returns false.

See also setNumericMode().

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

Returns true if s1 sorts before s2. This makes a collator usable as the comparison function of std::sort.

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

This is an overloaded function.

Returns true if s1 sorts before s2.

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

Assigns other to this collator and returns a reference to it.

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

Move-assigns other to this collator and returns a reference to it.

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

Sets the case sensitivity of the collator to cs. With Qt::CaseInsensitive, strings that differ only in case compare equal.

See also caseSensitivity().

void QCollator::setIgnorePunctuation(bool on)

Sets whether punctuation, symbols and white space are ignored to on.

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)

Sets numeric mode to on. In numeric mode, runs of digits are compared by their numeric value, so "2" sorts before "10".

See also numericMode().

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

Returns the sort key of string. Comparing sort keys gives the same result as compare() on their strings.

void QCollator::swap(QCollator &other)

Swaps this collator with other.