Sort article by date

lizhunter's Avatar

lizhunter

29 Jul, 2014 07:43 PM

I have a fairly basic script that should sort articles in folders by start-date. The sorting is not working. I am using this script on a different site and it is working fine. Any help would be appreciated. Here is what I have:

#set ($year = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block//system-folder[@current and not(@reference)]"))
#set ($articles = $_XPathTool.selectNodes($year, "system-folder[position() <= last()-1]/system-page[path != '/_internal/base-assets/article']"))

$_SortTool.addSortCriterion("start-date", "", "number", "ascending", "")
$_SortTool.sort($articles)

#foreach ($article in $articles)
#set ($startDate = $_DateTool.getDate($article.getChild("start-date").value))
$startDate<br />
#end

You can see here that the articles are not being sorted: http://news.siu.edu/2014/

Here, where the same script is applied, the articles are being sorted: http://coas.siu.edu/news/archive.html

When I test out just outputting the actual start date that is not transformed, they are not sorted.

  1. 1 Posted by Ryan Griffith on 30 Jul, 2014 04:54 PM

    Ryan Griffith's Avatar

    Hi,

    I was taking a look at the link you provided and it appears as though the events are being sorted. Were you able to figure things out with your Format?

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by lizhunter on 30 Jul, 2014 05:12 PM

    lizhunter's Avatar

    Hi Ryan. On news.siu.edu/2014<http://news.siu.edu/2014> the articles start with May, and then as you scroll down they aren't in order by month...and I believe One month appears twice.

    Sent from my iPhone

  3. 3 Posted by Ryan Griffith on 30 Jul, 2014 06:21 PM

    Ryan Griffith's Avatar

    Ah, my apologies for overlooking the incorrect ordering, I am seeing it now.

    When you have a moment, please attach your Format along with a sample of the XML being applied to it so I can do some local testing. The sample XML can be obtained by using the Preview Options when editing the Format.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by lizhunter on 30 Jul, 2014 07:02 PM

    lizhunter's Avatar

    Sure, here are the XML and format files. These are a little different from what I posted on the forum, as I was trying to shorten it up for that. This format is the full format. I have a part commented out where I was testing just outputting the date, if that helps any. Thanks.

  5. 5 Posted by Ryan Griffith on 31 Jul, 2014 01:46 PM

    Ryan Griffith's Avatar

    Hi,

    I believe the the sorting issue may be due to the fact that the pages are within month folders, which are also not ordered. I think the best solution would be to sort and loop over the months, and sort and loop over the articles within each month. I reworked your Format a bit and came up with the attached Format, which appears to be sorting correctly now.

    Note: you may need to tweak the sorting order for the months and/or articles depending on your needs.

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by lizhunter on 31 Jul, 2014 02:21 PM

    lizhunter's Avatar

    I was thinking that might be the problem but wasn’t coming up with a good solution. Thank you! I’ll check it out and let you know if I have questions.

    LIZ HUNTER
    Webmaster

    WEB COMMUNICATIONS
    UNIVERSITY COMMUNICATIONS
    MAIL CODE 6819
    SOUTHERN ILLINOIS UNIVERSITY
    1220 DOUGLAS DRIVE
    CARBONDALE, ILLINOIS 62901

    [email blocked]<mailto:[email blocked]>
    P: 618/453-2820

    SIU.EDU<http://siu.edu/>

  7. 7 Posted by Ryan Griffith on 31 Jul, 2014 05:29 PM

    Ryan Griffith's Avatar

    Sounds good, Liz, please keep me posted on how the updated Format works out.

    Thanks!

  8. 8 Posted by lizhunter on 31 Jul, 2014 07:53 PM

    lizhunter's Avatar

    Nearly perfect! Thanks! I just need to add an if statement to not show months that don’t have content yet (future months). I should be able to handle that. Thanks again. You’re always so helpful.

    LIZ HUNTER
    Webmaster

    WEB COMMUNICATIONS
    UNIVERSITY COMMUNICATIONS
    MAIL CODE 6819
    SOUTHERN ILLINOIS UNIVERSITY
    1220 DOUGLAS DRIVE
    CARBONDALE, ILLINOIS 62901

    [email blocked]<mailto:[email blocked]>
    P: 618/453-2820

    SIU.EDU<http://siu.edu/>

  9. 9 Posted by Ryan Griffith on 31 Jul, 2014 07:59 PM

    Ryan Griffith's Avatar

    Thank you for following up, Liz. I am glad to hear the updated Format did the trick.

    Regarding that #if statement. What you could do is move the #if ($articles.size() > 0) statement within the listMonthArticles macro to the listMonths macro:

    #macro(listMonths $months)
        #if ($months.size() > 0)
            $_SortTool.addSortCriterion("name", "", "number", "ascending", "")
            $_SortTool.sort($months)
            #foreach ( $m in $months )
                #set ( $articles = $_XPathTool.selectNodes($m, "system-page[path != '/_internal/base-assets/article' and title != '']") )
                #if ($articles.size() > 0)
                    #displayMonthHeading($m.getChild("path").value)
                    #listMonthArticles($articles)
                #end
            #end
        #end
    #end
    
    #macro(listMonthArticles $articles)
            $_SortTool.addSortCriterion("start-date", "", "number", "ascending", "")
            $_SortTool.sort($articles)
            
            <ul>
            #foreach ( $article in $articles )
                #set ( $startDate = $_DateTool.getDate($article.getChild("start-date").value) )
                <li>${_DateTool.format("MM/dd/yyyy", $startDate)}<a href="${article.getChild('path').value}">${_SerializerTool.serialize($article.getChild("title"), true)}</a></li>
            #end
            </ul>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  10. Ryan Griffith closed this discussion on 20 Aug, 2014 01:08 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