How to compare strings for sorting in typescript

In this typescript tutorial, we will see how to compare strings for sorting in typescript by using different methods. The methods are:

  • Using the localeCompare() method
  • Using the < and > operators
  • Using the localeCompare() method with options

Compare strings for sorting in typescript using localeCompare() method

Here we will see how to compare strings for sorting using the localeCompare() method in typescript.

In sorted order, the localeCompare method in typescript returns a number indicating if a reference string comes before, after, or is the same as the given string.

Syntax:

string.localeCompare( param )

For example, we will compare two string and returns a number pointing to their relative sort order by using localeCompare(). This method will return a negative number if the string comes before the second, and methods return zero, if they should sort the same. And the method returns a positive number if the first string must come after the second.

Open the code editor create a file named as ‘compareStringForSorting.ts’ file, write the below code:

const country = ['USA', 'UK', 'Australia'];
country.sort((a, b) => a.localeCompare(b));
console.log(country); 

To compile the code and run the below command and you can see the result in the console:

ts-node compareStringForSorting.ts
how to compare strings for sorting in typescript by using localeCompare()
how to compare strings for sorting in typescript by using localeCompare()

This is an example of how to compare strings for sorting in typescript by using localeCompare().

Compare strings for sorting in typescript using the < and > operators

Here we will see how to compare strings for sorting Using the < and > operators in typescript.

The less than(<) and greater than(>) operator checks whether the left operands is less than and greater than from the value of the right operands in the typescript. If the condition is satisfied, then it returns true or else false.

For example, by using the < and >’ operators, will provide true or false results depending on whether the left operand is less than or greater than the right operand, this method compares two strings.

In the compareStringForSorting.ts file write the below code:

const country = ['USA', 'UK', 'Australia'];
country.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
console.log(country); 

To compile the code and run the below command and you can see the result in the console:

ts-node compareStringForSorting.ts
how to compare strings for sorting in typescript by using greater and smaller than operators
how to compare strings for sorting in typescript by using < and > operators

This is an example of how to compare strings for sorting in typescript by using < and > operators.

Compare strings for sorting in typescript using localeCompare() method with options

Here we will see how to compare strings for sorting using the localeCompare() method with options in typescript.

In sorted order, the localeCompare method in typescript returns a number indicating if a reference string comes before, after, or is the same as the given string.

For example, this method is comparable to the first one but gives you the ability to specify parameters that influence the string comparison. For e.g., you can utilize settings to regulate the use of accent marks, numeric collation, and case sensitivity.

In the compareStringForSorting.ts file write the below code:

const country = ['USA', 'UK', 'Australia'];
country.sort((a, b) =>  a.localeCompare(b, undefined, { sensitivity: 'base' }));
console.log(country); 

To compile the code and run the below command and you can see the result in the console:

ts-node compareStringForSorting.ts
how to compare strings for sorting in typescript by using localeCompare method with options
how to compare strings for sorting in typescript by using localeCompare() method with options

This is an example of how to compare strings for sorting in typescript by using localeCompare() method with options

Conclusion

In this typescript tutorial, we saw how to compare strings for sorting in typescript by using different methods. These methods are:

  • Using the localeCompare() method
  • Using the < and > operators
  • Using the localeCompare() method with options

You may like the following typescript tutorials:

>