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

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

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

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:
- How to map an array of objects to another array of objects in Typescript
- Convert Typescript Array to String With Separator
- Get Key by Value from enum String in Typescript
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com