I would like to sort the RSS feed by most recent item and keep an item's title matched with its corresponding link. Here is how I parse and display the feed. The property I have thought would be best to do this is .isoDate, I am unsure about how to go about this process though.
I have gotten it to work by just pushing: item.isoDate + item.title into an array and sorting it, and doing the same for an array for item.isoDate + item.link. This however was a quick workaround, and doesn't always work with larger feeds.
function MarinersFeed() {
const [disable, setDisable] = React.useState(false);
const [completeFeed, setCompletedFeed] = React.useState([]);
const [feedTitle, setFeedTitle] = React.useState([]);
const [feedLink, setFeedLink] = React.useState([]);
React.useEffect(async () => {
let Parser = require('rss-parser');
let parser = new Parser();
const tFeed = [];
const tFeedTitle = [];
const tFeedLink = []
let feed = await parser.parseURL('https:*******.herokuapp.com/https://www.mlb.com/mariners/feeds/news/rss.xml');
feed.items.forEach(item => {
//sort by using item.isoDate, keep item.title and item.link connected
tFeed.push(item.title);
tFeedLink.push( item.link);
});
setCompletedFeed(tFeed);
//setFeedTitle(tFeedTitle);
setFeedLink(tFeedLink);
}, []);
const renderData = () => {
return completeFeed.map((f, index) => {
const title = feedTitle[index];
const link = feedLink[index]
const fullFeed = completeFeed[index]
return <div key={index} >
<h3>{fullFeed}
<Button onClick={(e) => {
e.preventDefault();
console.log(link)
window.open(link, "_blank")
}}></Button></h3>
</div>
})
}
return(
<>
{renderData()}
</>
)
}
export default MarinersFeed;
You can use the array sort method, for example
Output: