TypeScript - Array sort()

sort() method sorts the elements of an array.


array.sort( compareFunction );

Parameter Details

compareFunction − Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically.

Return Value

Returns a sorted array.


var arr = new Array("orange", "mango", "banana", "sugar"); 
var sorted = arr.sort(); 
console.log("Returned string is : " + sorted );

On compiling, it will generate the same code in JavaScript.

Its output is as follows −

Returned string is : banana,mango,orange,sugar
Kickstart Your Career

Get certified by completing the course

Get Started