Forum Discussion

Gursimrat's avatar
Gursimrat
Leader
11 years ago

Float Thread sorting and order

I am using the fllowing REST Api to fetch the floating threads, i need them to be sorted and like the newest thread created to be on top. 

 

/restapi/vc/categories/id/CAT-ID/subscriptions/global/float/thread

 

Thanks

  • AdamN's avatar
    AdamN
    11 years ago

    If I understand correctly, you want to get a list of floated threads sorted by the date of the root post. In your sample code, it looks like you're sorting messages within a thread, rather than sorting the list of threads.

     

    Here's a quick example of sorting the list of floated threads (community-wide) by the date of the root post (most recent first):

    <#assign floated_threads = rest("/subscriptions/global/float/thread").subscriptions />
    <#list floated_threads.subscription?sort_by(["target","messages","topic","post_time"])?reverse as subscription>
    ${subscription.target.messages.topic.id} - ${subscription.target.messages.topic.post_time} <br>
    </#list>

     So basically I get the list of floated threads across the entire community, iterate through the list sorted in reverse order by post_time, and then output the id and date of the root post.

     

    I want to point out that in this case the sort_by is taking a sequence of 4 values which correspond to the names of the nested subvariables. For example, the post_time of the root post as I'm looping through the subscriptions is accessible via "subscription.target.messages.topic.id", so my parameter for "sort_by" is the sequence "["target","messages","topic","post_time"]"

     

    I hope this helps!

  • AdamN's avatar
    AdamN
    Khoros Oracle

    Hi Gursimrat,

     

    I don't believe that particular REST API call supports any parameters for sorting; however, you should be able to sort the threads after the fact using the Freemarker "sort_by" built-in:

    http://freemarker.org/docs/ref_builtins_sequence.html#ref_builtin_sort_by

     

    Here's an example of how that works blogs:

    http://lithosphere.lithium.com/t5/developers-discussion/Sorting-blogs-by-alphabetical-order/m-p/38272#M647

     

    The approach should be similar for threads.

     

    I hope this helps!

    • Gursimrat's avatar
      Gursimrat
      Leader

      It is not working. I need to sort it based on "post_time

       

      My FTL Code

       

      <#assign floating_threads = rest("categories/id/10023/subscriptions/global/float/thread").subscriptions/>
      
      <#list floating_threads.subscription as fth>
      <#list fth.target.messages.linear.message?sort_by("post_time") as t> 
      <li>
      <a href="${t.@view_href}">${t.subject}</a>
      </li>
      </#list>
      </#list>

      It returns the following xml 

       

      <subscription type="subscription" href="/subscriptions/global/id/411">
      <id type="int">411</id>
      <target type="thread" href="/threads/id/5081">
      <id type="int">5081</id>
      <messages>
      <topic type="message" href="/messages/id/5081">
      <id type="int">5081</id>
      <read_only type="boolean">false</read_only>
      <thread type="thread" href="/threads/id/5081"/>
      <last_edit_author type="user" href="/users/id/25">
      <login type="string">The_Tackler</login>
      </last_edit_author>
      <parent type="message" null="true"/>
      <teaser type="string"/>
      <views>
      <count type="int">4</count>
      </views>
      <root type="message" href="/messages/id/5081"/>
      <board_id type="int">10</board_id>
      <post_time type="date_time">2013-10-10T12:34:10+00:00</post_time>
      <last_edit_time type="date_time">2013-10-10T12:34:10+00:00</last_edit_time>
      <subject type="string">Gaming and consoles support</subject>
      <deleted type="boolean">false</deleted>
      <author type="user" href="/users/id/25">
      <login type="string">The_Tackler</login>
      </author>
      <message_rating type="float">0.0</message_rating>
      <kudos>
      <count type="int">0</count>
      </kudos>
      <board type="board" href="/boards/id/22643"/>
      <labels/>
      </topic>
      <count type="int">1</count>
      <linear>
      <message type="message" href="/messages/id/5081">
      <id type="int">5081</id>
      <read_only type="boolean">false</read_only>
      <thread type="thread" href="/threads/id/5081"/>
      <last_edit_author type="user" href="/users/id/25">
      <login type="string">The_Tackler</login>
      </last_edit_author>
      <parent type="message" null="true"/>
      <teaser type="string"/>
      <views>
      <count type="int">4</count>
      </views>
      <root type="message" href="/messages/id/5081"/>
      <board_id type="int">10</board_id>
      <post_time type="date_time">2013-10-10T12:34:10+00:00</post_time>
      <last_edit_time type="date_time">2013-10-10T12:34:10+00:00</last_edit_time>
      <subject type="string">Testing the float support</subject>
      <deleted type="boolean">false</deleted>
      <author type="user" href="/users/id/25">
      <login type="string">The_Tackler</login>
      </author>
      <message_rating type="float">0.0</message_rating>
      <kudos>
      <count type="int">0</count>
      </kudos>
      <board type="board" href="/boards/id/22643"/>
      <labels/>
      </message>
      </linear>
      <read>
      <count type="int">1</count>
      </read>
      </messages>
      <solutions/>
      <title type="string" null="true"/>
      <board type="board" href="/boards/id/22643"/>
      <interaction_style type="string">board</interaction_style>
      </target>
      <target_type type="subscription_target_type">thread</target_type>
      <subscription_type type="subscription_type">float</subscription_type>
      <user type="user" null="true"/>
      </subscription>

       

      Can you let me know where the problem is?

      • AdamN's avatar
        AdamN
        Khoros Oracle

        If I understand correctly, you want to get a list of floated threads sorted by the date of the root post. In your sample code, it looks like you're sorting messages within a thread, rather than sorting the list of threads.

         

        Here's a quick example of sorting the list of floated threads (community-wide) by the date of the root post (most recent first):

        <#assign floated_threads = rest("/subscriptions/global/float/thread").subscriptions />
        <#list floated_threads.subscription?sort_by(["target","messages","topic","post_time"])?reverse as subscription>
        ${subscription.target.messages.topic.id} - ${subscription.target.messages.topic.post_time} <br>
        </#list>

         So basically I get the list of floated threads across the entire community, iterate through the list sorted in reverse order by post_time, and then output the id and date of the root post.

         

        I want to point out that in this case the sort_by is taking a sequence of 4 values which correspond to the names of the nested subvariables. For example, the post_time of the root post as I'm looping through the subscriptions is accessible via "subscription.target.messages.topic.id", so my parameter for "sort_by" is the sequence "["target","messages","topic","post_time"]"

         

        I hope this helps!