This project has moved. For the latest updates, please go here.

Sorting a feed RssDataSource

Topics: User Forum
Oct 23, 2007 at 10:30 PM
I have a feed that I am consuming that is coming in not sorted by publication date, and I want to display it sorted by publication date. How would I go about doing that, is this has been answered somewhere else please let me know thanks.

Eric
Oct 24, 2007 at 4:02 PM
In case someone else is trying to do this in the future here is the method that I have implemented:

feed.Rss.Channel.Items.Sort(new Classes.RssItemPubDateSort());

public class RssItemPubDateSort : IComparer<RssToolkit.Rss.RssItem>
{
public int Compare(RssToolkit.Rss.RssItem px, RssToolkit.Rss.RssItem py)
{
int retVal = 0;

if (px.PubDate == string.Empty && py.PubDate == string.Empty)
{
retVal = 0;
}
else if (px.PubDate != string.Empty && py.PubDate == string.Empty)
{
retVal = -1;
}
else if (px.PubDate == string.Empty && py.PubDate != string.Empty)
{
retVal = 1;
}
else
{
retVal = DateTime.Compare(Convert.ToDateTime(px.PubDate), Convert.ToDateTime(py.PubDate));
}

return retVal;
}
}

Eric
Feb 10, 2010 at 5:29 AM

Lovely - worked a treat.