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
const RssParser = require('rss-parser');
async function fetchRss({ url, sort = false, sortOrder = 'desc' } = {}) {
const rssParser = new RssParser();
const feed = await rssParser.parseURL(url);
if (sort) {
feed.items.sort((a, b) => {
return sortOrder === 'asc'
? new Date(a.isoDate) - new Date(b.isoDate) // Oldest first
: new Date(b.isoDate) - new Date(a.isoDate); // Newest first
});
}
return feed;
}
async function main() {
const output = await fetchRss({ url: 'https://www.mlb.com/mariners/feeds/news/rss.xml', sort: true });
console.log(output);
}
main();
Output:
{
items: [
{
creator: 'Jim Callis, Sam Dykstra and Jonathan Mayo',
title: '30 AFL sleeper prospects -- 1 for each org.',
link: 'https://www.mlb.com/mariners/news/sleeper-prospects-in-arizona-fall-league-2021',
pubDate: 'Fri, 15 Oct 2021 01:46:57 GMT',
'dc:creator': 'Jim Callis, Sam Dykstra and Jonathan Mayo',
guid: 'https://www.mlb.com/mariners/news/sleeper-prospects-in-arizona-fall-league-2021',
isoDate: '2021-10-15T01:46:57.000Z'
},
{
creator: 'Daniel Kramer',
title: 'J-Rod launches his own YouTube channel',
link: 'https://www.mlb.com/mariners/news/julio-rodriguez-mariners-top-prospect-launching-youtube-channel',
pubDate: 'Thu, 14 Oct 2021 14:31:30 GMT',
'dc:creator': 'Daniel Kramer',
guid: 'https://www.mlb.com/mariners/news/julio-rodriguez-mariners-top-prospect-launching-youtube-channel',
isoDate: '2021-10-14T14:31:30.000Z'
},
...