From 104817e2399376a1f05641cbcde9cffdb4071be3 Mon Sep 17 00:00:00 2001 From: VoodooSV Date: Mon, 5 Mar 2018 18:02:00 +0200 Subject: [PATCH] Fix compareNames compareNames doesn't work when it passes to Array.sort From docs: >The compare function should return a negative, zero, or positive value, depending on the arguments: --- lecture/4-lists-input/contacts.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lecture/4-lists-input/contacts.js b/lecture/4-lists-input/contacts.js index bc64806..b63c0cc 100644 --- a/lecture/4-lists-input/contacts.js +++ b/lecture/4-lists-input/contacts.js @@ -17,7 +17,11 @@ const generatePhoneNumber = () => `${rand(999, 100)}-${rand(999, 100)}-${rand(99 const createContact = () => ({name: generateName(), phone: generatePhoneNumber()}) // compare two contacts for alphabetizing -export const compareNames = (contact1, contact2) => contact1.name > contact2.name +export const compareNames = (contact1, contact2) => { + if (contact1.name < contact2.name) return -1 + if (contact1.name > contact2.name) return 1 + return 0 +} // add keys to based on index const addKeys = (val, key) => ({key, ...val})