From 5cb6ef07989175372f34bb97440dd96d412d4a8c Mon Sep 17 00:00:00 2001 From: Timo Schneider Date: Thu, 13 Jun 2024 09:54:10 +0200 Subject: [PATCH] added sort for posts by date --- src/App.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index ab15b29..d03979a 100644 --- a/src/App.js +++ b/src/App.js @@ -27,11 +27,18 @@ const App = () => { const reload = async () => { - await loadPosts().then(data => { - setPosts(data); + await loadPosts().then(posts => { + posts = recursiveSort(posts); + setPosts(posts); }); } + const recursiveSort = (posts) => { + posts.forEach((post, index) => {posts[index].comments = recursiveSort(post.comments)}); + posts.sort((a, b) => a.date < b.date); + return posts; + } + const loadPosts = async () => { const location = await locationUtils.getCurrentLocation(); return await postApi.getPostsByCoords(location.lon, location.lat);