JavaScript Intl Collator() Constructor

JavaScript Intl.Collator() constructor is used to create an Intl.Collator object

Syntax

new Intl.Collator(locales,option)

or

Intl.Collator(locales,option)

You can call the constructor with or without a new keyword.

Parameters

locales: A string or array of strings that represent the BCP 47 language tags.

options: It is an object that has many properties like localeMatcher, usage, numeric, sensitivity, caseFirst, etc.

Return Value

It returns an Intl.Collator object.

Example 1: Usage of Intl Collator() Constructor

const footballer = new Intl.Collator('en-US', { sensitivity: 'base' });
console.log(footballer.compare('ronaldo', 'RONALDO')); 
console.log(footballer.compare('ronaldo', 'MESSI')); 
console.log(footballer.compare('5', '7')); 
console.log(footballer.compare('true', 'false')); 

Output

0
1
-1
1

Example 2: Sorting with Uppercase and Lowercase

console.log(['ronaldo','MESSI','MBAPPE','mbappe','RONALDO','messi']
 .sort(new Intl.Collator('en-US', {caseFirst:'upper'}).compare));
console.log(['ronaldo','MESSI','MBAPPE','mbappe','RONALDO','messi']
 .sort(new Intl.Collator('en-US', {caseFirst:'lower'}).compare));

Output

[ 'MBAPPE', 'mbappe', 'MESSI', 'messi', 'RONALDO', 'ronaldo' ]
[ 'mbappe', 'MBAPPE', 'messi', 'MESSI', 'ronaldo', 'RONALDO' ]

Browser Compatibility

Chrome 24+ Edge 12+ Firefox 29+ Safari 10+ Opera 15+
Yes Yes Yes Yes Yes

That’s it!