1234567891011121314151617181920 |
- let persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- const mySort = (arr, sortField, ascDesc = true) => {
- const sortFunc = (a, b) => {
- if (a[sortField] < b[sortField]){
- return ascDesc ? -1 : 1;
- }
- return ascDesc ? 1 : -1;
- }
- return arr.sort(sortFunc);
- };
- console.log(mySort(persons, 'age'));
- console.log(mySort(persons, 'name', false));
|