QtBase
v6.3.1
|
The QCollator class compares strings according to a localized collation algorithm. More...
#include <qcollator.h>
Public Member Functions | |
QCollator () | |
QCollator (const QLocale &locale) | |
QCollator (const QCollator &) | |
~QCollator () | |
QCollator & | operator= (const QCollator &) |
QCollator (QCollator &&other) noexcept | |
void | swap (QCollator &other) noexcept |
void | setLocale (const QLocale &locale) |
QLocale | locale () const |
Qt::CaseSensitivity | caseSensitivity () const |
void | setCaseSensitivity (Qt::CaseSensitivity cs) |
void | setNumericMode (bool on) |
bool | numericMode () const |
void | setIgnorePunctuation (bool on) |
bool | ignorePunctuation () const |
int | compare (const QString &s1, const QString &s2) const |
int | compare (const QChar *s1, int len1, const QChar *s2, int len2) const |
bool | operator() (const QString &s1, const QString &s2) const |
int | compare (QStringView s1, QStringView s2) const |
bool | operator() (QStringView s1, QStringView s2) const |
QCollatorSortKey | sortKey (const QString &string) const |
Static Public Member Functions | |
static int | defaultCompare (QStringView s1, QStringView s2) |
static QCollatorSortKey | defaultSortKey (QStringView key) |
The QCollator class compares strings according to a localized collation algorithm.
\inmodule QtCore
\reentrant
QCollator is initialized with a QLocale. It can then be used to compare and sort strings in using the ordering appropriate to the locale.
A QCollator object can be used together with template-based sorting algorithms, such as std::sort(), to sort a list with QString entries.
In addition to the locale, several optional flags can be set that influence the result of the collation.
{setlocale(LC_COLLATE, nullptr)} would report) and the "C" locale. Definition at line 77 of file qcollator.h.
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.
Definition at line 112 of file qcollator.cpp.
Constructs a QCollator using the given locale.
Definition at line 123 of file qcollator.cpp.
Creates a copy of other.
Definition at line 131 of file qcollator.cpp.
QCollator::~QCollator | ( | ) |
Destroys this collator.
Definition at line 145 of file qcollator.cpp.
|
inlinenoexcept |
Move constructor. Moves from other into this collator.
Note that a moved-from QCollator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.
Definition at line 85 of file qcollator.h.
Qt::CaseSensitivity QCollator::caseSensitivity | ( | ) | const |
Returns case sensitivity of the collator.
This defaults to case-sensitive until set.
Definition at line 265 of file qcollator.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.
Definition at line 107 of file qcollator.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 105 of file qcollator.h.
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.
Definition at line 110 of file qcollator_icu.cpp.
|
static |
Compares the strings s1 and s2, returning their sorting order. This function performs the same operation as compare() on a default-constructed QCollator object.
Definition at line 380 of file qcollator.cpp.
|
static |
Returns the sort key for the string key. This function performs the same operation as sortKey() on a default-constructed QCollator object.
Definition at line 393 of file qcollator.cpp.
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.
Definition at line 321 of file qcollator.cpp.
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.
Definition at line 234 of file qcollator.cpp.
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.
Definition at line 294 of file qcollator.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 110 of file qcollator.h.
|
inline |
A QCollator can be used as the comparison function of a sorting algorithm. It returns true
if s1 sorts before s2, otherwise false
.
Definition at line 115 of file qcollator.h.
Assigns other to this collator.
Move-assigns from other to this collator.
Note that a moved-from QCollator can only be destroyed or assigned to. The effect of calling other functions than the destructor or one of the assignment operators is undefined.
Definition at line 154 of file qcollator.cpp.
void QCollator::setCaseSensitivity | ( | Qt::CaseSensitivity | cs | ) |
Sets the case-sensitivity of the collator to cs.
Definition at line 244 of file qcollator.cpp.
void QCollator::setIgnorePunctuation | ( | bool | on | ) |
Ignores punctuation and symbols if on is true
, attends to them if false
.
Definition at line 304 of file qcollator.cpp.
Sets the locale of the collator to locale.
Definition at line 217 of file qcollator.cpp.
void QCollator::setNumericMode | ( | bool | on | ) |
Enables numeric sorting mode when on is true
.
Definition at line 275 of file qcollator.cpp.
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.
Definition at line 129 of file qcollator_icu.cpp.
Swaps this collator with other. This function is very fast and never fails.
Definition at line 89 of file qcollator.h.