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
2
3
4
5 let numbers = [0, 1, 2, 3, 10, 20, 30];
6numbers.sort((a, b) => a - b);
7
8console.log(numbers);
1this.state.data.sort((a, b) => a.timeM > b.timeM ? 1:-1).map(
2 (item, i) => <div key={i}> {item.matchID} {item.timeM} {item.description}</div>
3)
1var arr = [23, 34343, 1, 5, 90, 9]
2
3//using forEach
4var sortedArr = [];
5arr.forEach(x => {
6 if (sortedArr.length == 0)
7 sortedArr.push(x)
8 else {
9 if (sortedArr[0] > x) sortedArr.unshift(x)
10 else if (sortedArr[sortedArr.length - 1] < x) sortedArr.push(x)
11 else sortedArr.splice(sortedArr.filter(y => y < x).length, 0, x)
12 }
13})
14console.log(sortedArr);
15
16
17// using sort method
18console.log(arr.sort((a,b)=>{
19 return a-b;
20}));
21