PySide6.QtCore.QCollator¶
- class QCollator¶
- The - QCollatorclass compares strings according to a localized collation algorithm. More…- Synopsis¶- Methods¶- def - __init__()
- def - compare()
- def - locale()
- def - numericMode()
- def - __call__()
- def - setLocale()
- def - setNumericMode()
- def - sortKey()
- def - swap()
 - Static functions¶- def - defaultCompare()
- def - defaultSortKey()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QCollatoris initialized with a- QLocale. It can then be used to compare and sort strings by using the ordering appropriate for that locale.- A - QCollatorobject can be used together with template-based sorting algorithms, such as std::sort(), to sort a list with- QStringentries.- def sortedStrings(seq): order = QCollator() 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: - Only the - c()and- system()locales are supported. Consult the POSIX and C Standard Library manuals for the- <locale.h>header for more information on the system locale.
- caseSensitivity()is not supported: only case-sensitive collation can be performed.
- numericMode()and- ignorePunctuation()are not supported.
 - The use of any of the unsupported options will cause a warning to be printed to the application’s output. - __init__()¶
 - Constructs a - QCollatorusing 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 - __init__(other)
- Parameters:
- other – - QCollator
 
 - Creates a copy of - other.- __init__(locale)
- Parameters:
- locale – - QLocale
 
 - Constructs a - QCollatorusing the given- locale.- See also - caseSensitivity()¶
- Return type:
 
 - 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 - compare(s1, s2)¶
- Parameters:
- s1 – str 
- s2 – str 
 
- Return type:
- int 
 
 - Compares - s1with- s2.- Returns a negative integer if - s1is less than- s2, a positive integer if it is greater than- s2, and zero if they are equal.- compare(s1, s2)
- Parameters:
- s1 – str 
- s2 – str 
 
- Return type:
- int 
 
 - This is an overloaded function. - compare(s1, len1, s2, len2)
- Parameters:
- s1 – - QChar
- len1 – int 
- s2 – - QChar
- len2 – int 
 
- Return type:
- int 
 
 - This is an overloaded function. - Compares - s1with- s2.- len1and- len2specify the lengths of the- QChararrays pointed to by- s1and- s2.- Returns a negative integer if - s1is 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.- static defaultCompare(s1, s2)¶
- Parameters:
- s1 – str 
- s2 – str 
 
- Return type:
- int 
 
 - Compares the strings - s1and- s2, returning their sorting order. This function performs the same operation as- compare()on a default-constructed- QCollatorobject.- See also - static defaultSortKey(key)¶
- Parameters:
- key – str 
- Return type:
 
 - Returns the sort key for the string - key. This function performs the same operation as- sortKey()on a default-constructed- QCollatorobject.- See also - ignorePunctuation()¶
- Return type:
- bool 
 
 - 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 - 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 - numericMode()¶
- Return type:
- bool 
 
 - Returns - trueif numeric sorting is enabled,- falseotherwise.- 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 - __call__(s1, s2)¶
- Parameters:
- s1 – str 
- s2 – str 
 
- Return type:
- bool 
 
 - A - QCollatorcan be used as the comparison function of a sorting algorithm. It returns- trueif- s1sorts before- s2, otherwise- false.- See also - __call__(s1, s2)
- Parameters:
- s1 – str 
- s2 – str 
 
- Return type:
- bool 
 
 - This is an overloaded function. - setCaseSensitivity(cs)¶
- Parameters:
- cs – - CaseSensitivity
 
 - Sets the case-sensitivity of the collator to - cs.- See also - setIgnorePunctuation(on)¶
- Parameters:
- on – bool 
 
 - Ignores punctuation and symbols if - onis- true, attends to them if- false.- See also - Sets the locale of the collator to - locale.- See also - setNumericMode(on)¶
- Parameters:
- on – bool 
 
 - Enables numeric sorting mode when - onis- true.- See also - sortKey(string)¶
- Parameters:
- string – str 
- Return type:
 
 - 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. - Swaps this collator with - other. This operation is very fast and never fails.