1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- const array = [11, 2, 8, 10, 1, 5, 3];
- array.sort();
- console.log(array);
- const months = ['March', 'Jan', 'Feb', 'Dec'];
- months.sort();
- console.log(months);
- array.sort((a, b) => a - b);
- console.log(`output->array`, array);
- array.sort((a, b) => b - a);
- console.log(`output->array`, array);
- const items = [
- { name: 'Edward', value: 21 },
- { name: 'Sharpe', value: 37 },
- { name: 'And', value: 45 },
- { name: 'The', value: -12 },
- { name: 'Magnetic', value: 13 },
- { name: 'Zeros', value: 37 },
- ];
- items.sort((a, b) => (a.name > b.name ? 1 : -1));
- console.log(items);
- items.sort((a, b) => a.value - b.value);
- console.log(items);
|