Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QCollator Class Reference

\inmodule QtCore More...

#include <qcollator.h>

+ Collaboration diagram for QCollator:

Public Member Functions

 QCollator ()
 
 QCollator (const QLocale &locale)
 Constructs a QCollator using the given locale.
 
 QCollator (const QCollator &)
 Creates a copy of other.
 
 ~QCollator ()
 Destroys this collator.
 
QCollatoroperator= (const QCollator &)
 Assigns other to this collator.
 
 QCollator (QCollator &&other) noexcept
 
void swap (QCollator &other) noexcept
 Swaps this collator with other.
 
void setLocale (const QLocale &locale)
 Sets the locale of the collator to locale.
 
QLocale locale () const
 Returns the locale of the collator.
 
Qt::CaseSensitivity caseSensitivity () const
 Returns case sensitivity of the collator.
 
void setCaseSensitivity (Qt::CaseSensitivity cs)
 Sets the case-sensitivity of the collator to cs.
 
void setNumericMode (bool on)
 Enables numeric sorting mode when on is true.
 
bool numericMode () const
 Returns true if numeric sorting is enabled, false otherwise.
 
void setIgnorePunctuation (bool on)
 Ignores punctuation and symbols if on is true, attends to them if false.
 
bool ignorePunctuation () const
 Returns whether punctuation and symbols are ignored when collating.
 
int compare (const QString &s1, const QString &s2) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int compare (const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
bool operator() (const QString &s1, const QString &s2) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int compare (QStringView s1, QStringView s2) const
 
bool operator() (QStringView s1, QStringView s2) const
 
QCollatorSortKey sortKey (const QString &string) const
 Returns a sortKey for string.
 

Static Public Member Functions

static int defaultCompare (QStringView s1, QStringView s2)
 
static QCollatorSortKey defaultSortKey (QStringView key)
 

Detailed Description

\inmodule QtCore

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

Since
5.2

\reentrant

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.

{
std::sort(seq.begin(), seq.end(), order);
return seq;
}
\inmodule QtCore
Definition qcollator.h:44
GLfixed GLfixed GLint GLint order
QStringList sortedStrings(QStringList seq)
[0]

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

Definition at line 43 of file qcollator.h.

Constructor & Destructor Documentation

◆ QCollator() [1/4]

QCollator::QCollator ( )
Since
5.13

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(), QLocale::setDefault()

Definition at line 93 of file qcollator.cpp.

References d.

◆ QCollator() [2/4]

QCollator::QCollator ( const QLocale & locale)
explicit

Constructs a QCollator using the given locale.

See also
setLocale()

Definition at line 104 of file qcollator.cpp.

◆ QCollator() [3/4]

QCollator::QCollator ( const QCollator & other)

Creates a copy of other.

Definition at line 112 of file qcollator.cpp.

References QCollatorPrivate::ensureInitialized(), QCollatorPrivate::ref, and QBasicAtomicInteger< T >::ref().

+ Here is the call graph for this function:

◆ ~QCollator()

QCollator::~QCollator ( )

Destroys this collator.

Definition at line 125 of file qcollator.cpp.

References QBasicAtomicInteger< T >::deref(), and QCollatorPrivate::ref.

+ Here is the call graph for this function:

◆ QCollator() [4/4]

QCollator::QCollator ( QCollator && other)
inlinenoexcept

Definition at line 51 of file qcollator.h.

References other().

+ Here is the call graph for this function:

Member Function Documentation

◆ caseSensitivity()

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()

Definition at line 244 of file qcollator.cpp.

References QCollatorPrivate::caseSensitivity.

◆ compare() [1/3]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.2

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}.

Definition at line 76 of file qcollator.h.

References compare(), and s2.

+ Here is the call graph for this function:

◆ compare() [2/3]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.2

Definition at line 70 of file qcollator.h.

References compare(), and s2.

Referenced by QFileSystemModelSorter::compareNodes(), and QDirSortItemComparator::compareStrings().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare() [3/3]

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

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.

Definition at line 74 of file qcollator_icu.cpp.

References QCollatorPrivate::caseSensitivity, QCollatorPrivate::collator, QtPrivate::compareStrings(), QCollatorPrivate::ensureInitialized(), and s2.

+ Here is the call graph for this function:

◆ defaultCompare()

int QCollator::defaultCompare ( QStringView s1,
QStringView s2 )
static
Since
6.3

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

See also
compare(), defaultSortKey()

Definition at line 362 of file qcollator.cpp.

References s2.

◆ defaultSortKey()

QCollatorSortKey QCollator::defaultSortKey ( QStringView key)
static
Since
6.3

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

See also
sortKey(), defaultCompare()

Definition at line 375 of file qcollator.cpp.

◆ ignorePunctuation()

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()

Definition at line 300 of file qcollator.cpp.

References QCollatorPrivate::ignorePunctuation.

◆ locale()

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(), QLocale::collation()

Definition at line 213 of file qcollator.cpp.

References QCollatorPrivate::locale.

Referenced by setLocale().

+ Here is the caller graph for this function:

◆ numericMode()

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()

Definition at line 273 of file qcollator.cpp.

References QCollatorPrivate::numericMode.

◆ operator()() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
5.2

Definition at line 79 of file qcollator.h.

References compare(), and s2.

+ Here is the call graph for this function:

◆ operator()() [2/2]

bool QCollator::operator() ( QStringView s1,
QStringView s2 ) const
inline
Since
5.13

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()

Definition at line 83 of file qcollator.h.

References compare(), and s2.

+ Here is the call graph for this function:

◆ operator=()

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

Assigns other to this collator.

Move-assigns other to this QCollator instance.

\fn QCollator::QCollator(QCollator &&other)

Move constructor. Moves from \a other into this collator.

! [partially-formed]

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. ! [partially-formed]

partially-formed

Definition at line 134 of file qcollator.cpp.

References QBasicAtomicInteger< T >::deref(), QCollatorPrivate::ensureInitialized(), other(), QCollatorPrivate::ref, and QBasicAtomicInteger< T >::ref().

+ Here is the call graph for this function:

◆ setCaseSensitivity()

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

Sets the case-sensitivity of the collator to cs.

See also
caseSensitivity()

Definition at line 223 of file qcollator.cpp.

References QCollatorPrivate::caseSensitivity.

Referenced by QDirSortItemComparator::QDirSortItemComparator(), and QFileSystemModelSorter::QFileSystemModelSorter().

+ Here is the caller graph for this function:

◆ setIgnorePunctuation()

void QCollator::setIgnorePunctuation ( bool on)

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

See also
ignorePunctuation()

Definition at line 283 of file qcollator.cpp.

References QCollatorPrivate::ignorePunctuation.

◆ setLocale()

void QCollator::setLocale ( const QLocale & locale)

Sets the locale of the collator to locale.

See also
locale()

Definition at line 196 of file qcollator.cpp.

References locale(), and QCollatorPrivate::locale.

+ Here is the call graph for this function:

◆ setNumericMode()

void QCollator::setNumericMode ( bool on)

Enables numeric sorting mode when on is true.

See also
numericMode()

Definition at line 254 of file qcollator.cpp.

References QCollatorPrivate::numericMode.

Referenced by QFileSystemModelSorter::QFileSystemModelSorter().

+ Here is the caller graph for this function:

◆ sortKey()

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.

Definition at line 93 of file qcollator_icu.cpp.

References QCollatorPrivate::collator, QCollatorPrivate::ensureInitialized(), QCollatorPrivate::isC(), QByteArray(), and Qt::Uninitialized.

+ Here is the call graph for this function:

◆ swap()

void QCollator::swap ( QCollator & other)
inlinenoexcept

Swaps this collator with other.

This function is very fast and never fails.

Definition at line 55 of file qcollator.h.

References d, other(), and qt_ptr_swap().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: