Forum Discussion

vishwajeet_hol's avatar
10 years ago

Sort the posts by the number of views to that topic

Hi,

 

I am trying to sort posts by no. of views.

 

Just tried below code by referering one of the post on lithosphere, but it doesn't works :

http://community.lithium.com/t5/Developers-Discussion/sorting-forum-posts-by-the-number-of-messages-in-each-post/m-p/199561#M8702

 

 

<#assign thread_list=rest("/boards/id/<board_ID>/threads").threads.thread/>
<ul>
<#list thread_list?sort_by(['messages','views','count'])?reverse as thread>
<li>${thread.id}</li>
</#list>
</ul>   

 Let me know if there is any way.

 

Thank you.

 

Regards,

Vishwajeet

 

  • vishwajeet_hol Updated your code a bit, see if this works

     

    <#assign thread_list=rest("/boards/id/<board_ID>/threads").threads/>
    <ul>
    <#list thread_list.thread?sort_by(['messages','topic','views','count'])?reverse as thread>
    <li>${thread.id}</li>
    </#list>
    </ul>  

     

     

  • vishwajeet_hol Updated your code a bit, see if this works

     

    <#assign thread_list=rest("/boards/id/<board_ID>/threads").threads/>
    <ul>
    <#list thread_list.thread?sort_by(['messages','topic','views','count'])?reverse as thread>
    <li>${thread.id}</li>
    </#list>
    </ul>  

     

     

      • NicoB's avatar
        NicoB
        Lithium Alumni (Retired)

        Hi vishwajeet_hol

        altough VarunGrazitti solution technically works I woul heartly refrain from using it.

        What you are doing with that approach is to query for a (potentially big) set of data and ordering them in memory using the application server resources.

        Imagine that code executed by 1000 people simultaneously.. and you're community is likely to start suffering.

         

        I would personally use REST API v2 if you could:

         

        SELECT * FROM messages WHERE depth=0 AND board.id="<ID>" order by metrics.views DESC

        Thanks,

         

        Nico