Skip to content

Instantly share code, notes, and snippets.

@abhinavguptas
Created October 5, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhinavguptas/58f9fe53a1bf91fb71a8bcd41fa89605 to your computer and use it in GitHub Desktop.
Save abhinavguptas/58f9fe53a1bf91fb71a8bcd41fa89605 to your computer and use it in GitHub Desktop.
#salesforce #Winter24 release came with Collator class, that is meant to provide locale sensitive sorting. This video gives a hands-on demo of the class, along with areas where its meant to be used, and where not to use it.
@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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment