1numArray.sort((a, b) => a - b); // For ascending sort
2numArray.sort((a, b) => b - a); // For descending sort
3
1var items = [
2 { name: 'Edward', value: 21 },
3 { name: 'Sharpe', value: 37 },
4 { name: 'And', value: 45 },
5 { name: 'The', value: -12 },
6 { name: 'Magnetic', value: 13 },
7 { name: 'Zeros', value: 37 }
8];
9
10// sort by value
11items.sort(function (a, b) {
12 return a.value - b.value;
13});
14
15// sort by name
16items.sort(function(a, b) {
17 var nameA = a.name.toUpperCase(); // ignore upper and lowercase
18 var nameB = b.name.toUpperCase(); // ignore upper and lowercase
19 if (nameA < nameB) {
20 return -1;
21 }
22 if (nameA > nameB) {
23 return 1;
24 }
25
26 // names must be equal
27 return 0;
28});
1let numbers = [4, 10, 5, 1, 3];
2numbers.sort((a, b) => a - b);
3console.log(numbers);
4
5// [1, 2, 3, 4, 5]
1const points = [40, 100, 1, 5, 25, 10];
2points.sort(function(a, b){return a - b}); // for ascending
3//another way for ascending
4points.sort((a,b)=>{
5 if(a>b){
6 return 1
7 }else if(a<b){
8 return -1
9 }else{
10 return 0
11 }
12})
13points.sort(function(a, b){return b - a}); // for descending
14//another way for descending
15points.sort((a,b)=>{
16 if(a>b){
17 return -1
18 }else if(a<b){
19 return 1
20 }else{
21 return 0
22 }
23})
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)