1// Sort an array of numbers
2let numbers = [5, 13, 1, 44, 32, 15, 500]
3
4// Lowest to highest
5let lowestToHighest = numbers.sort((a, b) => a - b);
6//Output: [1,5,13,15,32,44,500]
7
8//Highest to lowest
9let highestToLowest = numbers.sort((a, b) => b-a);
10//Output: [500,44,32,15,13,5,1]
11
1// ascending and discending for number
2const arr1 = [21, 2100, 2, 35000];
3const arr2 = [21, 2100, 2, 35000];
4
5let ascN = arr1.sort((f, s) => f - s);
6let dscN = arr2.sort((f, s) => s - f);
7
8// ascending and discending for string
9const arr3 = ['21', '2100', '2', '35000'];
10const arr4 = ['21', '2100', '2', '35000'];
11
12let ascS = arr3.sort((f, s) => f.length - s.length);
13let dscS = arr4.sort((f, s) => s.length - f.length);
1
2
3
4
5 let numbers = [0, 1, 2, 3, 10, 20, 30];
6numbers.sort((a, b) => a - b);
7
8console.log(numbers);