Forum Discussion

vishwajeet_hol's avatar
11 years ago
Solved

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>  

     

     

5 Replies

  • 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's avatar
    vishwajeet_hol
    Expert
    11 years ago

    Hi VarunGrazitti,

     

    Found one issue.

    I observed that, ID's I am getting in the response are not sorted by number of views to that topic.

    If so, then by which criteria they are getting sorted.

     

    Thank you.

     

    Regards,

    Vishwajeet.

  • NicoB's avatar
    NicoB
    Lithium Alumni (Retired)
    11 years ago

    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