Ordering an rss feed

steelej's Avatar

steelej

27 Aug, 2014 06:34 PM

We are using rss feeds form LiveWhale calendar. For some reason (or lack of reason) their rss feeds come in reverse chronological order. I've been working with the sort tool to try and reverse this order without luck as of yet. Any suggestions?

  1. 1 Posted by Ryan Griffith on 28 Aug, 2014 12:25 PM

    Ryan Griffith's Avatar

    Hi Jim,

    When you have a moment, please also provide your Format at this point and I will be happy to help take a look.

    Thanks!

  2. 2 Posted by steelej on 28 Aug, 2014 01:10 PM

    steelej's Avatar

    Here you go. Thanks.

    Jim

  3. 3 Posted by Ryan Griffith on 28 Aug, 2014 01:29 PM

    Ryan Griffith's Avatar

    Hi Jim,

    Have you tried something like the following to sort the articles?

    ## grab all news items from RSS feed
    #set ( $articles = $_XPathTool.selectNodes($contentRoot,"channel//item") )
    
    #if ($articles.size() > 0)
        $_SortTool.addSortCriterion("pubDate", "", "text", "ascending", "upper-first")  
        $_SortTool.sort($articles)
    
        #foreach ( $article in $articles )
            ...
        #end
    #end
    

    I suspect there may be issues with sorting the pubDate's format correctly since it's a string and not a timestamp. If that is the case, you may not be able to use the Sort Tool, but some other means. Keep me posted and we'll go down that path.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by steelej on 28 Aug, 2014 01:37 PM

    steelej's Avatar

    Tried it. No go. Didn’t make sense that it would.

    What I did try was:

    #if ($articles.size() > 0)

        $_SortTool.addSortCriterion("", "", "", "ascending", "")

        $_SortTool.sort($articles)

    Just to see if I could switch the order. Not successful. I did contact the calendar vendor to see why they thought it useful to render the rss in this way.

    I’ve been thinking of maybe setting a count and trying to use that to reverse the order but I haven’t gotten my head around that yet.

    Jim

  5. 5 Posted by Ryan Griffith on 28 Aug, 2014 02:06 PM

    Ryan Griffith's Avatar

    Hi Jim,

    That's what I suspected would happen. What we may be able to do is generate an array of objects (Java Maps) that contains a key with a timestamp representation of the pubDate. Then, use the native Velocity SortTool to sort by the timestamp.

    When you have a moment, try something like the following:

    ## grab all news items from RSS feed
    #set ( $articles = $_XPathTool.selectNodes($contentRoot,"channel//item") )
    
    #if ($articles.size() > 0)
        #set ( $articleArr = [] )
        #foreach ( $article in $articles )
            #set ( $pubDateTimestamp = $_DateTool.toDate("EEE, dd MMM yyyy HH:mm:ss zzz", $article.getChild('pubDate').value) )
            #set ( $_void = $articleArr.push({
                "title": "${_EscapeTool.xml($article.getChild('title').value)}"
                "description": "${_EscapeTool.xml($article.getChild('description').value)}"
                "pubDate": "${article.getChild('pubDate').value}",
                "pubDateTimestamp": $pubDateTimestamp.getTime()
            }) )
        #end
    
        #set ( $articleArr = $_SortTool.sort($articleArr, "pubDateTimestamp:desc") )
        #foreach ( $article in $articleArr )
            $article.get("title")
            $article.get("description")
            $article.get("pubDate")
        #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  6. Ryan Griffith closed this discussion on 12 Sep, 2014 07:54 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac