Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alexandru 112 posts 272 karma points
    Oct 04, 2013 @ 10:57
    Alexandru
    0

    Counting and sorting comments issue

    I am trying to list the headlines of the 4 most popular posts (posts with most comments).

    This is the macro I have managed to adapt to my needs: I have taken what I have found on a previous post and changed it so that it would fit my blog.

    <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost">
        <xsl:sort select="count(BlogLibrary:GetCommentsForPost(@id)//comment)" order="descending" />
          <xsl:if test="position()&lt; 4">
                    <xsl:variable name="text" select="umbraco.library:StripHtml($currentPage/@nodeName)" />            
                    <a href="{umbraco.library:NiceUrl(@id)}" class="popularPostsClass"><xsl:value-of select="./@nodeName" disable-output-escaping="yes"/></a>
                    <br/>
          </xsl:if>   
          <xsl:if test="position()= 4">
                    <xsl:variable name="text" select="umbraco.library:StripHtml($currentPage/@nodeName)" />            
                    <a href="{umbraco.library:NiceUrl(@id)}" class="popularPostsClass"><xsl:value-of select="./@nodeName" disable-output-escaping="yes"/></a>
          </xsl:if>
      </xsl:for-each>
    

    There is a problem though, the sorting is not properly done. The first item on my list is a post that has 14 comments, the second has 24, the third has 3 and the fourth has 2.

    This is not the correct order and it seems to read strange values because as I add comments to certain posts, they start getting up on the list, even ahead of the ones with 14 or 24 comments.

    I think, however, that the problem lies behind the fact that somehow,,, the ID's of the comments get in the way and instead of counting the actual comments, it counts the ID of the comment?... I am not sure...

    Can anyone help?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 04, 2013 @ 12:25
    Chriztian Steinmeier
    100

    Hi Alexandru,

    Sorting defaults to "text" mode (e.g. "10" comes before "2") - so you just need to add the data-type attribute to the <xsl:sort> instruction:

    <xsl:sort select="count(BlogLibrary:GetCommentsForPost(@id)//comment)" data-type="number" order="descending" />
    

    /Chriztian

  • Alexandru 112 posts 272 karma points
    Oct 04, 2013 @ 12:27
    Alexandru
    0

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft