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:
 
RWUConversionContext context("UTF-8"); //1
 
RWUString array[] = { //2
"United States",
"Netherlands",
"United Kingdom",
"Germany",
"France",
"Italy",
"Japan",
"Australia",
""
};
 
RWUCollator collator; //3
RWTValSortedVector<RWUCollationKey, //4
std::less<RWUCollationKey> > vector;
int i;
for (i = 0; array[i].isNull() == false; ++i) {
vector.insert(collator.getCollationKey(array[i])); //5
}
 
for (i = 0; i < vector.entries(); ++i) { //6
std::cout << " " << vector[i].getString() << std::endl;
}
 
//1 Indicates that source and target strings are encoded as UTF-8.
//2 Creates an array of country names.
//3 Creates a collator based on the default locale.
//4 Creates an empty vector. An RWTValSortedVector<T> automatically keeps its contents in sorted order.
//5 Populates the vector with RWUCollationKey objects. Note that the vector uses RWUCollationKey::operator<() and operator==() for sorting.
//6 Prints 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.