in Android Code Tips

How to sort RSS by date

So, if you’ve got one or more RSS feeds and you want to sort them according to the pubdate, do this:

Collections.sort(myFeedsList, new Comparator<RssItemModel>() {

            @Override
            public int compare(RssItemModel lhs, RssItemModel rhs) {
                SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
                try {
                    Date date1 = formatter.parse(rhs.getPubDate());
                    Date date2 = formatter.parse(lhs.getPubDate());
                    return date1.compareTo(date2);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
Share