Performing Language-Sensitive String Comparison

Use ICollation to perform language-sensitive comparison of two Unicode strings. You can use the collation object for the language of the current default locale, or you can specify a particular language or locale.

  1. Call ICollation::createCollation to get the collation object for the locale you want.
    You can indicate both a specific locale and an ordering strength for the collation object. If you don't specify a locale, the functions returns the collation object for the current default locale. If you don't specify an ordering strength, the default is ICollation::kTertiaryDifference.

ICollation::createCollation returns a host-specific object if one is available. If one does not exist for the specified locale, it returns a portable object instantiated with the specified locale.

  1. Call ICollation::compare to compare the two strings.
    compare returns an enum value indicating the results of the comparison: kSourceLess (-1), kSourceEqual (0), or kSourceGreater (1). You can also use the functions isEqual, isGreaterThan, or isLessThan. These return a bool.

For example, this code shows how to compare two strings, text1 and text2, using the collation object for the French locale:

// Create a locale key for the French locale

ILocaleKey french((IText("FR_FR"));

// Create the collation object, using the default ordering 
strength

ICollation* order = ICollation::createCollation(french);

// Compare the strings

ICollation::ETextComparisonResult result = order-
>compare(text1, text2);

if (result == ICollation::kSourceEqual) 

	// strings are equal

	else if (result == ICollation::kSourceLess) 

		// text1 is less than text2

		else

			// text1 is greater than text2

delete order;

This code shows how to use isEqual to compare the strings, using the collation order for the default locale:

ICollation* order = ICollation::createCollation();

if (order->isEqual(text1, text2)) 

	// strings are equal

delete order;


Overview of Locale Classes
Locale Names
Collation Classes
Ordering Strength