Forum Discussion

nickyates's avatar
nickyates
Mentor
12 years ago

sorting forum posts by the number of messages in each post

Hi,

 

I have a category called help (categories/id/5678).  Within help there are several 7 forums.  I would like to display the names of the forums along with how many messages in total there are in each forum ordered by the number of messages.  currently my code looks like this:

 

<#assign boards = rest("categories/id/5678")>

<table class="lia-list-wide">
<thead id="columns">
<tr>
<th scope="col" >Title</th>
<th scope="col" ">Comments</th>
</tr>
</thead>
<tbody>

<#list boards.category.boards.board as c>
<tr>
<td colspan="1" rowspan="1">
<h3><a id="link_13" id="link_13">${c.title}</a></h3>
<div>${c.description}</div>
</td>
<td rowspan="1" colspan="1">
Message count here
</td>
</tr>
</#list>
</tbody>
</table>

 

I had a similar question here (https://lithosphere.lithium.com/t5/developers-discussion/sorting-forum-posts-by-the-number-of-messages-in-each-post/m-p/81670#M2848) which helped on a similar issue but i just cant seem to get this to work using that code example.

 

Thanks!

  • Hi Yuri,

     

    That was exactly what i was after, i did make one small change, i added in ?number after /messages/count").value as it was treating the numbers as text and was putting the boards in a slightly wrong order.

     

    <#assign boards = rest("categories/id/5678/boards").boards/>

    <#assign order_list = []/>
    <#list boards.board as c>
    <#assign one_index_obj = {"count":rest("/boards/id/${c.id}/messages/count").value?number, "spot":c_index}/>
    <#assign order_list = order_list + [one_index_obj]/>
    </#list>

    <#list order_list?sort_by("count")?reverse as list_item>
    ${list_item.count}:${list_item.spot}<p>
    ${boards.board[list_item.spot].title}<p>
    </#list>

     

    Thanks Yuri, this has been a massive help

6 Replies

  • YuriK's avatar
    YuriK
    Khoros Expert
    12 years ago

    Hey nickyates,

     

    You can use the id from each board to make another rest api call. In the example below, all I did is call the following API on each board:

     

    http://lithosphere.lithium.com/t5/rest-api/bd-p/developers-rest-api?leaf-id=Board.messages.count#Board.messages.count

     

    <#assign boards = rest("categories/id/5678")>
    <table class="lia-list-wide">
    <thead id="columns">
    <tr>
    <th scope="col" >Title</th>
    <th scope="col" ">Comments</th>
    </tr>
    </thead>
    <tbody>
    
    <#list boards.category.boards.board as c>
    <tr>
    <td colspan="1" rowspan="1">
    <h3><a id="link_13" id="link_13">${c.title}</a></h3>
    <div>${c.description}</div>
    </td>
    <td rowspan="1" colspan="1">
    ${rest("/boards/id/${c.id}/messages/count").value}
    </td>
    </tr>
    </#list>
    </tbody>
    </table>

     

    Most Objects returned in the rest calls have either an ID and/or an href that you can then use to make api calls on that Object. 

     

    Hope this helps,

     

    Yuri

  • nickyates's avatar
    nickyates
    Mentor
    12 years ago

    Hi Yuri,

     

    Thanks for your response,

     

    My problem is that i want to order these by the number of messages on each board.  As i understand it you need to order at the level of the list by adding ?sort_by

     

    <#list boards.category.boards.board as c>

     

    however at this point i do not know the number of messages in each board so i am not able to order them.  

     

    Thanks

     

    Nick

     

     

  • SeanA's avatar
    SeanA
    Lithium Alumni (Retired)
    12 years ago

    I believe what Yuri is suggesting is to do this in two passes:

     

    • first rest call to get the boards
    • for each board, second api call to get the count of threads
    • store the count and ID in a local sequence,
    • sort it descending by count (might have to investigate the right object to use to do this, i'm a novice with freemarker)
    • loop through that and display the board info

    i haven't written the code for this but i *think* this should work. i can try it later today unless you beat me to it ;)

  • YuriK's avatar
    YuriK
    Khoros Expert
    12 years ago

    Hey nickyates,

     

    I created some code to create a list of the indexes of the boards in a list along with their count. You can then get the different pieces of information you need from each board to display. I just displayed the board title along with index and count. It's not the most efficient algorithm, but I can't think of anything better at this point with just using freemarker templates.

     

    <#assign boards = rest("categories/id/Management/boards").boards/>
    
    <#assign order_list = []/>
    <#list boards.board as c>
       <#assign one_index_obj = {"count":rest("/boards/id/${c.id}/messages/count").value, "spot":c_index}/>
       <#assign order_list = order_list + [one_index_obj]/>
    </#list>
    
    <#list order_list?sort_by("count")?reverse as list_item>
       ${list_item.count}:${list_item.spot}
       ${boards.board[list_item.spot].title}
    </#list>
    

    Hope this helps,

     

    Yuri

  • nickyates's avatar
    nickyates
    Mentor
    12 years ago

    Hi Yuri,

     

    That was exactly what i was after, i did make one small change, i added in ?number after /messages/count").value as it was treating the numbers as text and was putting the boards in a slightly wrong order.

     

    <#assign boards = rest("categories/id/5678/boards").boards/>

    <#assign order_list = []/>
    <#list boards.board as c>
    <#assign one_index_obj = {"count":rest("/boards/id/${c.id}/messages/count").value?number, "spot":c_index}/>
    <#assign order_list = order_list + [one_index_obj]/>
    </#list>

    <#list order_list?sort_by("count")?reverse as list_item>
    ${list_item.count}:${list_item.spot}<p>
    ${boards.board[list_item.spot].title}<p>
    </#list>

     

    Thanks Yuri, this has been a massive help