![]() |
To sort numerical string in TypeScript, we could use localCompare method or convert numerical string to number. Below are the approaches used to sort numerical string in TypeScript: Table of Content Approach 1: Using localeCompareThe localeCompare() is an inbuilt function in TypeScript that is used to get the number indicating whether a reference string comes before or after or is the same as the given string in sorted order. Syntax:string.localeCompare( param )
Example: Here, In this example, we are using LocalCompare() method. Javascript
Output: Before Sorting" Approach 2: Converting to Numbers before sortingHere, we convert the strings to numbers using Number() before sorting. The subtraction inside the sort function compares them numerically. SyntaxnumericalStrings.sort((a, b) => Number(a) - Number(b));
Example: Here, we are converting numerical string to numbers before sorting. Javascript
Output: Before Sorting |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |