Sorting an array
The sort
method modifies an array in place (i.e. it does not create a new array) by arranging elements according to an ordering function.
An ordering function returns a number which indicates the relative ordering of two values.
Loading TypeScript...
Notice that array
was modified by the sort
operation as well. We can prevent this behavior by first copying to a new array, then sorting the resulting array.
Loading TypeScript...
TypeScript also provides a Readonly
type guard which can ensure that an array is not modified.
Loading TypeScript...