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' ]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Jun, 2020
Like Article
Save Article