Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Internationalization Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

6.4 Using Collation Keys

An RWUCollationKey stores preprocessed comparison information for a Unicode string, and can be used to speed repeated string comparisons; for example, when sorting a set of strings. The RWUCollator class calculates collation weights incrementally. This ensures good performance, as most strings differ in their first few characters. However, if string comparisons are to be made repeatedly, then best performance can be achieved by obtaining an RWUCollationKey for each string and comparing the keys.


NOTE -- Collation keys can be compared meaningfully only if they are created by the same collator, or by collators with the same attributes.

6.4.1 Using RWUCollationKey

You can obtain a collation key for a given string using the getCollationKey() method on RWUCollator. For example:

The returned key may be compared to other keys produced by collators with the same attributes. Note that generating a key using RWUCollator::getCollationKey() not a trivial operation, as it involves determining the collation elements and weights for an entire string. Comparing two RWUCollationKey objects, however, is fast.

Collation keys can be compared using logical operators ==, !=, and <. Thus:

Collation keys can also be inserted into collections that use operators ==, !=, and < to order items. For example:

To get back the string associated with a collation key, use the getString() method:

6.4.2 Sorting Strings with Collation Keys

A common use of collation keys is in sorting a set of strings, where string comparisons are made repeatedly, as demonstrated in the code below:

//1Indicates that source and target strings are encoded as UTF-8.
//2Creates an array of country names.
//3Creates a collator based on the default locale.
//4Creates an empty vector. An RWTValSortedVector<T> automatically keeps its contents in sorted order.
//5Populates the vector with RWUCollationKey objects. Note that the vector uses RWUCollationKey::operator<() and operator==() for sorting.
//6Prints the sorted vector. Obtains each RWUString object from the stored RWUCollationKey object using getString(), implicitly converts it to UTF-8, and prints the UTF-8.


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.