array?.sort((a, b) => (a.price > b.price ? 1 : -1))
array?.sort((a, b) => (a.price > b.price ? -1 : 1))
array?.sort((a, b) => (a.name > b.name ? 1 : 1))
array?.sort((a, b) => (a.name > b.name ? -1 : 1))
array.sort((a,b) => new Date(b.date) - new Date(a.date));
const subjects = [
{ "name": "Math", "score": 81 },
{ "name": "English", "score": 77 },
{ "name": "Chemistry", "score": 87 },
{ "name": "Physics", "score": 84 }
];
subjects.sort((a, b) => (a.name > b.name) ? 1: -1);
console.log(subjects);
const stringArr = ["Joe", "Kapil", "Steve", "Musk"]
stringArr.sort();
(4) ["Joe", "Kapil", "Musk", "Steve"]
stringArr.reverse();
(4) ["Steve", "Musk", "Kapil", "Joe"]
### Sort Number Array in javascript
const array = [40, 100, 1, 5, 25, 10];
array.sort((a,b) => a-b);
(6) [1, 5, 10, 25, 40, 100]
array.sort((a,b) => b-a);
(6) [100, 40, 25, 10, 5, 1]