To sort an array in ascending order in TypeScript, you can use the `sort()` method similar to JavaScript.
In this example,
numbers
.sort()
method to sort the array in-place.let numbers: number[] = [5, 2, 8, 1, 9];
numbers.sort((a, b) => a - b);
console.log('Sorted Array of Integers:', numbers);
Sorted Array of Integers: [ 1, 2, 5, 8, 9 ]
In this example,
words
.sort()
method to sort the array in-place based on string comparison.let words: string[] = ['apple', 'banana', 'cherry', 'date'];
words.sort();
console.log('Sorted Array of Strings:', words);
Sorted Array of Strings: [ 'apple', 'banana', 'cherry', 'date' ]
In this tutorial, we learned How to Sort an Array in Ascending Order in TypeScript language with well detailed examples.