Press ESC to close

How to Use Apex Collator Class for Locale-Sensitive Sorting in Salesforce

Salesforce Winter’24 release came with Collator class, that is meant to provide locale sensitive sorting.

Locale-sensitive sorting refers to the process of sorting strings or other data types based on the rules and conventions of a specific locale. A locale is a set of parameters that defines the user’s language, region, and any special variant preferences that the user wants to see in their user interface. Different locales have different rules for how strings are sorted and compared.

For example, in English, the sorting order is generally straightforward and follows the ASCII values of characters. However, in languages like German, characters like “ä”, “ö”, and “ü” have special sorting rules. Similarly, in some Asian languages, the sorting order can be quite complex.

Java provides the Collator class in the java.text package for locale-sensitive sorting. You can create an instance of Collator for a specific locale and then use it to compare strings in a way that is appropriate for that locale. Similar Collator class is now available in Salesforce with Winter’24 release.

Hands-on Demo

This short < 6 mins video gives a demo of the Collator class and explains how, and when it should be used, along with tips on where it shouldn’t be used.

Code Snippet

You can run this code snippet in your Winter’24 Sandbox or pre-release org, needs API version 59.0 and beyond.

@istest
public class TestCollator {
/*
In German, characters like "ä", "ö", and "ü"
have specific sorting rules.
*/
static final String[] germanWords = new String[] {'Apfel', 'Unter', 'Auto', 'Öl', 'äpfel'};
/*
In French, characters like "é", "è", "ê", and "ë"
have specific sorting rules that differ from their base character "e".
*/
static final String[] frenchWords = new String[] {'école', 'élève', 'eclair', 'écrit', 'effort'};
static User runAsUser = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
TimeZoneSidKey='America/Los_Angeles',
ProfileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id,
UserName='standarduser' + DateTime.now().getTime() + '@testorg.com');
@IsTest
static void frenchUserLocaleSort() {
string userLocale = 'fr_FR';
runAsUser.LocaleSidKey=userLocale;
System.runAs(runAsUser) {
frenchWords.sort();
System.debug (' French Sorting – Without Collator – ' + frenchWords);
// Sort based on user Locale
Collator myCollator = Collator.getInstance();
frenchWords.sort(myCollator);
System.debug (' French Sorting – With Collator – ' + frenchWords);
}
}
@IsTest
static void germanUserLocaleSort() {
string userLocale = 'en_DE';
runAsUser.LocaleSidKey=userLocale;
System.runAs(runAsUser) {
germanWords.sort();
System.debug (' German Sorting – Without Collator – ' + germanWords);
// Sort based on user Locale
Collator myCollator = Collator.getInstance();
germanWords.sort(myCollator);
System.debug (' German Sorting – With Collator – ' + germanWords);
}
}
}

References

Leave a Reply

%d bloggers like this: