We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
import"h8k-components";import{useState,useEffect}from"react";importArticlesfrom"./components/Articles";import"./App.css";functionApp({articles}){const[filteredArticles,setFilteredArticles]=useState(articles||[]);consthandleMostUpvoted=()=>{// Logic for most upvoted articlessetFilteredArticles([...articles]?.sort((a,b)=>(b?.upvotes||0)-(a?.upvotes||0)));};consthandleMostRecent=()=>{// Logic for most recent articlessetFilteredArticles([...articles]?.sort((a,b)=>newDate(b?.date)-newDate(a?.date)));};useEffect(()=>{handleMostUpvoted();},[])return(<><h8k-navbarheader="Sorting Articles"></h8k-navbar><divclassName="App"><divclassName="layout-row align-items-center justify-content-center my-20 navigation"><labelclassName="form-hint mb-0 text-uppercase font-weight-light">SortBy</label><buttondata-testid="most-upvoted-link"className="small"onClick={handleMostUpvoted}>MostUpvoted</button><buttondata-testid="most-recent-link"className="small"onClick={handleMostRecent}>MostRecent</button></div><Articlesarticles={filteredArticles}/></div></>);}exportdefaultApp;
Cookie support is required to access HackerRank
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 →
Solution: