---
schema_version: "1"
id: 5390934be2ba7b44bef964fac94f2294290b3d3aac52daf41abcf61740a630ec
title: Collator Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_class_System_Collator.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Collator.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 9683ea79a04013971860270c20554ed408ca280a7e15d2a42e4a2e1d97391309
description: Contains methods to get locale-specific instances that can be used for comparisons and sorting. Use the getInstance() method to obtain the Collator instance for a given locale and pass the Collator as the Comparator parameter to the list.sort() method.Perform string comparisons for a given locale.Gets the Collator instance for the current user’s locale.
---
# Collator Class

Contains methods to get locale-specific instances that can be used for comparisons and sorting. Use the getInstance() method to obtain the Collator instance for a given locale and pass the Collator as the Comparator parameter to the list.sort() method.

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## Usage

Because locale-sensitive sorting can produce different results depending on the user running the code, avoid using it in triggers or in code that expects a particular sort order.

## Example

This example performs a default list sort and then uses Collator to sort based on the user locale.

@IsTest
    static void userLocaleSort() {
        
        string userLocale = 'fr\_FR';
        
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en\_US',
        LocaleSidKey=userLocale, TimeZoneSidKey='America/Los\_Angeles',
        ProfileId = \[SELECT Id FROM Profile WHERE Name='Standard User' WITH USER\_MODE\].Id,
        UserName='standarduser' + DateTime.now().getTime() + '@testorg.com');
       
        System.runAs(u) {
           
            List<String\> shoppingList = new List<String\> {
                'épaule désosé Agneau',
                'Juice',
                'à la mélasse Galette 5 kg',
                'Bread',
                'Grocery'
            };
            
            // Default sort
            shoppingList.sort();
            Assert.areEqual('Bread', shoppingList\[0\]);
            
            // Sort based on user Locale
            Collator myCollator = Collator.getInstance();
            shoppingList.sort(myCollator);
            Assert.areEqual('à la mélasse Galette 5 kg', shoppingList\[0\]);
            Assert.areEqual('Bread', shoppingList\[1\]);
            Assert.areEqual('épaule désosé Agneau', shoppingList\[2\]);
            Assert.areEqual('Grocery', shoppingList\[3\]);
            Assert.areEqual('Juice', shoppingList\[4\]);
        }
    }

- **[Collator Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Collator.htm#apex_System_Collator_methods)**

## Collator Methods

The following are methods for Collator.

- **[compare(source, target)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Collator.htm#apex_System_Collator_compare)**  
    Perform string comparisons for a given locale.
- **[getInstance()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Collator.htm#apex_System_Collator_getInstance)**  
    Gets the Collator instance for the current user’s locale.

### compare(source, target)

Perform string comparisons for a given locale.

#### Signature

public Integer compare(String source, String target)

#### Parameters

- **source**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **target**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

### getInstance()

Gets the Collator instance for the current user’s locale.

#### Signature

public static System.Collator getInstance()

#### Return Value

Type: [Collator Class](#apex_class_System_Collator "Contains methods to get locale-specific instances that can be used for comparisons and sorting. Use the getInstance() method to obtain the Collator instance for a given locale and pass the Collator as the Comparator parameter to the list.sort() method.")