diff --git a/src/App.js b/src/App.js index de6f15c..ef62f4c 100644 --- a/src/App.js +++ b/src/App.js @@ -9,8 +9,9 @@ function App() { const sortedNames = (names, key) => { return names.sort((a, b) => a[key].localeCompare(b[key])); }; - const [searchInput, setSearchInput] = useState(""); + const initialData = sortedNames(babyNamesData, "name"); + const [searchInput, setSearchInput] = useState(""); const [mainList, setMainList] = useState(initialData) return ( diff --git a/src/display-the-names.js b/src/display-the-names.js index cc8e05b..2547e90 100644 --- a/src/display-the-names.js +++ b/src/display-the-names.js @@ -13,7 +13,13 @@ const DisplayNames = ({ allNames, setMainList }) => { const handleDelete = (name) => { setFavoriteName(favoriteName.filter((babyName) => babyName.name !== name.name)) - setMainList(allNames.concat(name)) + let sortedNamesAfterFavorite = (names, key) => { + return names.sort((a, b) => a[key].localeCompare(b[key])); + }; + + const allNamesAfterFavorite = sortedNamesAfterFavorite([...allNames, name], "name"); + setMainList(allNamesAfterFavorite) + } console.log({ favoriteName })