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 "./App.css";
import { useState } from "react";
function App({ articles }) {
const [sortedArticles, setSortedArticles] = useState(
[...articles].sort((a, b) => b.upvotes - a.upvotes)
);
const handleMostUpvoted = () => {
// Logic for most upvoted articles
const updated = [...sortedArticles].sort((a, b) => b.upvotes - a.upvotes);
setSortedArticles(updated);
};
const handleMostRecent = () => {
// Logic for most recent articles
const updated = [...sortedArticles].sort(
(a, b) => new Date(b.date) - new Date(a.date)
);
setSortedArticles(updated);
};
return (
<>
Sort By
Most Upvoted
Most Recent
);
}
export default App;
import React from "react";
function Articles({ articles }) {
return (
Title
Upvotes
Date
Article Sorting
You are viewing a single comment's thread. Return to all comments →
All tests passed:
import "h8k-components";
import Articles from "./components/Articles";
import "./App.css"; import { useState } from "react";
function App({ articles }) { const [sortedArticles, setSortedArticles] = useState( [...articles].sort((a, b) => b.upvotes - a.upvotes) );
const handleMostUpvoted = () => { // Logic for most upvoted articles const updated = [...sortedArticles].sort((a, b) => b.upvotes - a.upvotes); setSortedArticles(updated); };
const handleMostRecent = () => { // Logic for most recent articles const updated = [...sortedArticles].sort( (a, b) => new Date(b.date) - new Date(a.date) ); setSortedArticles(updated); }; return ( <> Sort By Most Upvoted Most Recent ); }
export default App;
import React from "react";
function Articles({ articles }) { return ( Title Upvotes Date
); }
export default Articles;