To sort the given array of numbers in typescript, we can use the sort
function available in the Array
object. Here is how it can be done:
index.ts115 chars4 lines
The sort
function takes a compare function as an optional parameter that determines the sorting order. The compare function takes two arguments (a and b) and returns a negative number if a should come before b, a positive number if b should come before a, and 0 if their order does not matter.
In this case, we use the arrow function (a, b) => a - b
to sort the array in ascending order. For descending order, we can simply use (a, b) => b - a
.
gistlibby LogSnag