[Home] > Snippets  > Languages  > JavaScript  > Arrays  >  Sort an array of numbers

Sort an array of numbers

JavaScript

const sort = (arr) => arr.sort((a, b) => a - b)

TypeScript

const sort = (arr: number[]): number[] => arr.sort((a, b) => a - b)

Example

sort([1, 5, 2, 4, 3]) // [1, 2, 3, 4, 5]