You are viewing a single comment's thread. Return to all comments →
My solution, const handleMostUpvoted = () => { // Logic for most upvoted articles SetArticles(prevState => [...prevState].sort((a,b) =>b.upvotes- a.upvotes)); };
const handleMostRecent = () => { // Logic for most recent articles SetArticles(prevState => [...prevState].sort((a,b) =>new Date(b.date) - new Date(a.date))); };
{articles && articles.map((article,index) => { return ( <tbody> <tr data-testid="article" key= {`index-${article.title}`}> <td data-testid="article-title">{article.title}</td> <td data-testid="article-upvotes">{article.upvotes}</td> <td data-testid="article-date">{article.date}</td> </tr> </tbody> ) })}
Seems like cookies are disabled on this browser, please enable them to open this website
Article Sorting
You are viewing a single comment's thread. Return to all comments →
My solution, const handleMostUpvoted = () => { // Logic for most upvoted articles SetArticles(prevState => [...prevState].sort((a,b) =>b.upvotes- a.upvotes)); };
const handleMostRecent = () => { // Logic for most recent articles SetArticles(prevState => [...prevState].sort((a,b) =>new Date(b.date) - new Date(a.date))); };