The Array.sort() is an inbuilt TypeScript function which is used to sort the elements of an array.
Syntax:
array.sort( compareFunction )
Parameter: This method accept a single parameter as mentioned above and described below:
- compareFunction : This parameter is the function that defines the sort order
Return Value: This method returns the sorted array.
Below example illustrate the Array sort() method in TypeScriptJS:
Example 1:
JavaScript
<script>
var arr = [ 11, 89, 23, 7, 98 ];
var val = arr.sort();
console.log( val );
</script>
|
Output:
[ 11, 23, 7, 89, 98 ]
Example 2:
JavaScript
<script>
var arr = [ "G" , "e" , "e" , "k" , "s" , "f" , "o" , "r" ,
"g" , "e" , "e" , "k" , "s" ];
var val;
val = arr.sort();
console.log( val );
</script>
|
Output:
[ 'G', 'e', 'e', 'e', 'e', 'f', 'g', 'k', 'k', 'o', 'r', 's', 's' ]