A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-intl-collator-compare-method/ below:

JavaScript Intl Collator compare() Method

JavaScript Intl Collator compare() Method

Last Updated : 23 Jul, 2025

This intl.collator.prototype.compare() method basically used to make the comparison between two strings as per the sorting order of the collator object.

Here the compare-getter function gives a number as first-string and second-string compared between two strings as per the sorting order of the collator object, It shows a value less than 0 if the first string comes before the second string, shows a value greater than 0 if the first string comes after the second string and 0 if both the parametric strings are equalized.

Syntax:

collator.compare(firststring, secondstring)

Parameters: This method will take two strings (firststring, secondstring) as input parameters to make the comparison between them.

Example 1: For sorting array using compare-getter function which is bounded to follow the collator which further passed directly to Array.prototype.sort()

JavaScript
<script>
  var x = ['Geeks', 'Geeksfor', 'GFG', 'courses', 'java'];
  var collator = new Intl.Collator('de-u-co-phonebk');

  // Using collator compare function
  x.sort(collator.compare);
  console.log(x.join(', '));
</script>

Output: 

courses, Geeks, Geeksfor, GFG, java

Example 2: Also use the compare-getter function to search the matching strings between the given parametric strings. Let's see how:

JavaScript
<script>
  var x = ['GFG-for-GFG', 'stress', 'Care', 'surprise', 'gfg'];
  var collator = new Intl.Collator('fr', 
                     { usage: 'search', sensitivity: 'base' });

  var srch = 'gfg';
  var mtchs = x.filter(n => collator.compare(n, srch) === 0);
  console.log(mtchs.join(', '));
</script>

Output: 

gfg

Supported Browsers:

We have a complete list of Javascript Intl methods, to check those please go through this JavaScript Intl Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4