Browse Source

<HW8>upd done

Mark 1 year ago
parent
commit
cddcade758
1 changed files with 7 additions and 2 deletions
  1. 7 2
      08/main.js

+ 7 - 2
08/main.js

@@ -5,8 +5,13 @@ var persons = [
    { name: "Алексей", age: 73 },
    { name: "Яков", age: 12 },
 ]
-function personSort(key) {
-   persons.sort((a, b) => a[key] > b[key] ? 1 : -1)
+function sort(persons, key, ok = true) {
+   return persons.sort((a, b) => {
+      let result;
+      if (ok) { result = a[key] > b[key] ? 1 : -1; }
+      else { result = a[key] < b[key] ? 1 : -1; }
+      return result;
+   })
 }
 console.log(sort(persons, "age"));
 ///////////////////////////////////////////////