1// Sort Numbers in Descending Order
2function sortDescending(num) {
3 return Number(num.toString().split('').sort((a, b) => b - a).join(''));
4//or: return parseInt(num.toString().split('').sort().reverse().join(''));
5}
6
7console.log(sortDescending(123)); // 321
8console.log(sortDescending(1254859723)); // 9875543221
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);
1var numArray = [140000, 32, 12, 63323, 104, 99];
2
3numArray.sort(function(a, b) {
4 return b - a;
5});
6
7// Array(6) [ 140000, 63323, 104, 99, 32, 12 ]