Nono.MA

Sorting a List or Array in TypeScript

NOVEMBER 13, 2018

Sorting a list of numbers1

let numbers = [-34, 100, 20, 2, 3, 5, 6];
numbers.sort( (a, b) => a - b ); // Ascending sort
// [ -34, 2, 3, 5, 6, 20, 100 ]
numbers.sort( (a, b) => b - a ); // Descending sort
// [ 100, 20, 6, 5, 3, 2, -34 ]