1 function compareValues(key, order = 'asc') {
2 return function innerSort(a, b) {
3 if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
4 // property doesn't exist on either object
5 return 0;
6 }
7
8 const varA = (typeof a[key] === 'string')
9 ? a[key].toUpperCase() : a[key];
10 const varB = (typeof b[key] === 'string')
11 ? b[key].toUpperCase() : b[key];
12
13 let comparison = 0;
14 if (varA > varB) {
15 comparison = 1;
16 } else if (varA < varB) {
17 comparison = -1;
18 }
19 return (
20 (order === 'desc') ? (comparison * -1) : comparison
21 );
22 };
23}
1javascript sort object by value descending
2
3
4starWarsPeople.map(e=>{
5 return e
6 }).sort((a,b)=>{
7 if (a.height-b.height>1) {return -1;}
8 else if (a.height-b.height<1) {return 1;}
9 else {return 0;}
10 });
11