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"
import Articles from "./components/Articles";
import "./App.css";
function App({ articles }) {
const [sortArtiles, setSortArticles] = useState([])
const handleMostUpvoted = () => {
// Logic for most upvoted articles
let nexsortArtiles = [...articles]?.sort((a, b) => (b.upvotes) - (a.upvotes))
setSortArticles(nexsortArtiles)
};
const handleMostRecent = () => {
// Logic for most recent articles
let nexsortArtiles = [...articles]?.sort((a, b) => new Date(b.date) - new Date(a.date))
setSortArticles(nexsortArtiles)
};
useEffect(() => {
if (articles.length) {
setSortArticles([...articles].sort((a, b) => b.upvotes - a.upvotes));
}
}, [articles])
return (
<>
Sort By
Most Upvoted
Most Recent
);
}
export default App;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Article Sorting
You are viewing a single comment's thread. Return to all comments →
import "h8k-components"; import { useState, useEffect } from "react"
import Articles from "./components/Articles";
import "./App.css";
function App({ articles }) {
const [sortArtiles, setSortArticles] = useState([]) const handleMostUpvoted = () => { // Logic for most upvoted articles let nexsortArtiles = [...articles]?.sort((a, b) => (b.upvotes) - (a.upvotes)) setSortArticles(nexsortArtiles) };
const handleMostRecent = () => { // Logic for most recent articles let nexsortArtiles = [...articles]?.sort((a, b) => new Date(b.date) - new Date(a.date)) setSortArticles(nexsortArtiles) };
useEffect(() => { if (articles.length) { setSortArticles([...articles].sort((a, b) => b.upvotes - a.upvotes)); } }, [articles]) return ( <> Sort By Most Upvoted Most Recent ); }
export default App;