JavaScript Intl Collator() Constructor
JavaScript Intl.Collator() constructor is used to create an Intl.Collator object. This constructor can be called with or without the new keyword.
Syntax:
new Intl.Collator(loc, opt)
Parameters: This constructor takes two parameters and both are optional.
- loc: This is a String or an array of Strings with the following values allowed:
- co: Specifies the variation collants to be used
- kn: Specifies the numeric collations to be used
- kf: Specifies whether uppercase or lowercase sorting should be used.
- opt: It is an object which has some properties like localeMatcher, usage, caseFirst etc.
Returns: An Intl.Collator object.
Below examples illustrate the JavaScript Intl Collator() Constructor:
Example 1: This example implements the basic Collator constructor and uses its compare method
Javascript
console.log( new Intl.Collator().compare( 'b' , 's' )); console.log( new Intl.Collator().compare(4, 2)); console.log( new Intl.Collator().compare( true , true )); |
Output:
-1 1 0
Example 2: This example performs a modified sorting by using the Intl constructor
Javascript
console.log([ 'a' , 'A' , 'b' , 'B' ] .sort( new Intl.Collator( "en" , {caseFirst: "upper" }).compare)); console.log([ 'a' , 'A' , 'b' , 'B' ] .sort( new Intl.Collator( "en" , {caseFirst: "lower" }).compare)); |
Output:
(4) ['A', 'a', 'B', 'b'] (4) ['a', 'A', 'b', 'B']
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
We have a complete list of JavaScript Intl methods to check those please go through, the JavaScript Intl Reference article.
Contact Us