diff --git a/src/App.js b/src/App.js index 3784575..2e590db 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,69 @@ -import logo from './logo.svg'; -import './App.css'; +import React, { useState } from "react"; +import "./App.css"; +import Articles from "./components/Articles"; + +const title = "React Sorting articles"; + +function App({ articles }) { + const [articlesList, setArticlesList] = useState(articles); + + const sortByUpvotes = () => { + var newArticles = []; + Object.assign(newArticles, articlesList); + newArticles.sort((a, b) => { + if (a.upvotes > b.upvotes) { + return -1; + } + if (a.upvotes < b.upvotes) { + return 1; + } + return 0; + }); + + setArticlesList(newArticles); + }; + + const sortByDates = () => { + var newArticles = []; + Object.assign(newArticles, articlesList); + newArticles.sort((a, b) => { + const aDate = new Date(a.date); + const bDate = new Date(b.date); + if (aDate > bDate) { + return -1; + } + if (aDate < bDate) { + return 1; + } + return 0; + }); + + setArticlesList(newArticles); + }; -function App() { return (
-
- logo -

- Edit src/App.js and save to reload. -

- {title} +
+ + +
+ Most Recent + +
+ ); }