Is it possible to reverse the order in which comments appear?
Asked by Norman Hills:

Bizarre request I know, but, I've been asked if it is possible to reverse the order of comments, ie.e. have the most recent at the top?


Answers:
author: David at David Simpson Apps
body: You can do that client-side with a quick JavaScript hack...

Browse to **Confluence Admin \| Look \& Feel \| Custom HTML**

Add this to **At the end of the HEAD**:

```
<script>  AJS.toInit(function () {    $comments = AJS.$('#page-comments');     $comments.children().each(function (i,li) {
      $comments.prepend(li);
    });  });</script>
```

Check the [related blogpost](http://davidsimpson.me/2012/01/24/how-to-reverse-the-order-of-comments-on-confluence-pages/) for more tips :)

comments: author: Norman Hills
body: Thanks Dave, that did it.

Cheers

Norman


author: David at David Simpson Apps
body: No problem. If it worked, please mark it as correct :-)


author: David at David Simpson Apps
body: Also, if you want to allow the sort order to be changed, update the JavaScript to something like...

```
function reverseCommentOrder () {  var $comments = AJS.$('#page-comments');   $comments.children().each(function(i,li){$comments.prepend(li)});}AJS.toInit(function ($) {  reverseCommentOrder();  $('#comments-section-title').append(
    '<a id="page-comments-reverse" href="#">(Reverse Order)</a>'
  );    $('#page-comments-reverse')
    .css({ 'color':'#999','font-size':'0.65em'})
    .click(function (e) {      reverseCommentOrder();      e.preventDefault();    });});
```


author: David at David Simpson Apps
body: This doesn't change the ordering in any nested comments -- I'm not really sure what the correct behaviour would be there. Thoughts?


author: Norman Hills
body: Actually it sorta makes sense this way, the top level comments are presented in revese order and the replies are nested in reply order under the original - this seems pretty clear to me, I don't think I'd want the replies reversed as well.


author: David at David Simpson Apps
body: As I suspected. I couldn't really figure out what else would work.


author: Sean King
body: This works great for the first comment, but what about the subcomments underneath if people reply?


author: Sibel Yasar
body: Hi,

How can I get to the area **Confluence Admin \| Look \& Feel \| Custom HTML???**

Thanks in advance

Sibel


author: Nic Brough -Adaptavist-
body: Click on the admin cog, then look for look and feel, and custom html in the admin menu.


author: Victor Prasad
body: For the sort order, anyway I can do that for only 1 space?

Thanks,

V


author: David at David Simpson Apps
body: Sure, try this:

```
<script>

  var mySpaceKey = 'EXAMPLE'; // Change this to the space key for the 1 space
  AJS.toInit(function(){
    if (AJS.params.spaceKey == mySpaceKey) {      $comments = AJS.$('#page-comments');       $comments.children().each(function(i,li){$comments.prepend(li)});
    }  });</script>
```


author: Sibel Yasar
body: Thank you David!


author: George Lewe (LSY)
body: Indeed, the above solution only sorts the top level comments, not the sub comments. The reason is that only the top comment container has an identifier ("page-comments"). Such a global identifier can only be assigned to something that is always present. That is not the case for the sub-comments. So the sub-comments IDs are created on the fly and a solution like the above cannot know them beforehand of course.

Also, inserting the toggle link into the comment block has the flaw that it is removed when Confluence re-creates it after a new comment is added. Only a page refresh will make it appear again.

Other than that it works. Here is a solution how you can do this with a user macro so you can apply it to certain pages only by adding it onto the page.

```
## Macro title: Reverse Comments## Macro body processing: No macro body#### Created by: George Lewe## Created on: 2015-11-06#### This macro toggles page comments sort order#### @noparams<script>function rev(i, el) {   $comments = AJS.$('#page-comments');   $comments.prepend(el)}function reverseCommentOrder() {   if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform"){      $comments = AJS.$('#page-comments');      $comments.children().each(rev)   }}AJS.toInit(function() {   if($("#page-comments-reverse").length == 0) {      AJS.$('#comments-section-title').append('<a id="page-comments-reverse" href="#" onclick="reverseCommentOrder();" style="color: #999; font-size: 0.65em;">[Toggle order...]</a>;');   }});</script>
```

comments: author: Victor Prasad
body: where do would I insert this in a Space?

I am using 5.7.1.

Thanks!


author: George Lewe (LSY)
body: The solution above is a page macro. It only applies to the page when you insert it. It is not space-wide.


author: Ian
body: I have placed the above code in the template of my user macro but when I save the page where the macro is it spits out the code and thus not ordering the comments?

How can I make the code be executed and not spit on the page?


author: Arti Kumar
body: Did you figure this out?


author: George Lewe (LSY)
body: Hi all,

I noticed that the editor here replace \< with \&lt, and \> with \&gt; in the code I provided. I edited the code section accordingly.

We still use that macro successfully. The user macro is set to "No macro body".

Best regards,  
George


author: Ronald Horner
body: If you want to do this per space simply wrap it in {html} tags and place it in the custom header area: Space Admin -\> Look And Feel -\> Themes -\> Configure Theme -\> Header.

```
{html}<script>  AJS.toInit(function(){    $comments = AJS.$('#page-comments');     $comments.children().each(function(i,li){$comments.prepend(li)})  });</script>{html}
```

comments: author: Sibel Yasar
body: Thank you Ronald !


author: Ronald Horner
body: This solution will handle new comments and replies to those comments

```
&lt;script&gt;AJS.toInit(function(){   if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform" &amp;&amp; AJS.$('meta[name=ajs-form-name]').attr("content") != "threadedcommentform"){      // Only reverse comments if you're not editing one of them      $comments = AJS.$('#page-comments');      $comments.children().each(function(i,li){$comments.prepend(li)});    }});&lt;/script&gt;
```


author: Victor Prasad
body: Is this still viable for v 6.15.9 and above?

The Custom HTML inserted via the Admin console - does not seem to be working, but per space it does!

comments: author: David at David Simpson Apps
body: Try now. I've just updated my old answers from 2012 - when Atlassian migrated the content from the old community (answers.atlassian.com), some html was mistakenly escaped.


author: Victor Prasad
body: Thanks David!

Really appreciate that!

Is that the same for the one with the choice of sort order? add \<script\> tags?

V


author: Jazon Reyes
body: I'm not sure if this thread is still alive. I tried all the codes above, they all work. The thing is, everytime i enter a new comment, it is still displayed at the bottom. Any possible way that the newly entered comment(without refreshing the page) can appear on top as well?


author: Victor Prasad
body: Can I do that at just the Space Level, not a a global change?

Using v 5.7.1 Space Admin -\> Look And Feel -\> Themes -\> Configure Theme -\> Header does not exist. I get the option of current theme or choose new theme and confirm.

I notice there is a Blog Layout in the Space Tool-\>Look and Feel-\>Blog Post Layout

Guess I can just put that in there?


author: Robert Nordstrom
body: Is it possible to reverse the comments order in the On Demand version of Confluence? We have 145 comments On one of our meeting notes page that grows daily... Scrolling to the bottom is becoming a bit of a pain.Thanks,Robert

comments: author: karthik ses
body: Hi Robert,

Any update on this issue . Please let us know, we are also searching for the same


author: Robert Nordstrom
body: No, Karthik.

We have found no solution for reversing the sort order in the On Demand/cloud version of confluence. This is just one of the many limitations that we found since moving to the cloud instance. Seems like it should be a relatively minor thing to fix. Apparently, it's not high on the Atlassian scorecard.


author: karthik ses
body: Hi Robert,

But we need to get by adding java script on header side, I think It is also not work out. We are finding any changes. Please suggest if you have any alternate method.

Thanks


author: Nic Brough -Adaptavist-
body: There is no way to do it on Cloud. AS you say, you need to change or add some javascript. Which you can't do on Cloud.


author: Sean King
body: This works great for the first comment, but what about the subcomments underneath if people reply?


author: Mike McCracken
body: This still has issues when you try and reply to a comment. Can anyone help?

Is there an issue to have Atlassian make this part of the code functionality?

Thanks


author: Mike McCracken
body: I am having the same issue with the Documentation theme in Confluence 4.1.9. I cannot edit comments. It brings up the editor, does not show the existing comment an does not give you a cursor. The preview button does not work either.


author: Aviram Gabay
body: Hi, I am using confluence V3.5 .

I tried to use David's solution which worked great but now it seems that

I am unable to reply on an existing comment using rich text editor, only by wiki markup (test on Chrome).

did someone else had this issue? is there a solution for this?

comments: author: David at David Simpson Apps
body: I've not tried this in Confluence 3.5 only 4.x. Sorry.


author: David at David Simpson Apps
body: Try this to fix the comment edit/reply problem...

```
<script>  AJS.toInit(function ($) {    if ($('meta[name=ajs-form-name]').attr("content") != "editcommentform"){      // Only reverse comments if you're not editing one of them      $comments = $('#page-comments');      $comments.children()
        .each(function (i, li) {
          $comments.prepend(li);
        });    }  });</script>
```


author: Mike McCracken
body: This is excellent David. Seems to work.

So the complete script is:

```
&lt;script&gt;function reverseCommentOrder(){    if (AJS.$('meta[name=ajs-form-name]').attr("content") != "editcommentform"){      // Only reverse comments if you're not editing one of them      $comments = AJS.$('#page-comments');      $comments.children().each(function(i,li){$comments.prepend(li)});    }}AJS.toInit(function(){  reverseCommentOrder();  AJS.$('#comments-section-title').append('&lt;a id="page-comments-reverse" href="#"&gt;(Reverse Order)&lt;/a&gt;');    AJS.$('#page-comments-reverse').css({ 'color':'#999','font-size':'0.65em'}).click(function(e){    reverseCommentOrder();    e.preventDefault();  });});&lt;/script&gt;
```


author: Steve Schoo
body: Has anyone attempted something similar with Questions? A reverse sort or a way of maintaining Question order is what I'm looking for.


author: thejpkotor
body: Is there a way to just add the reverse link without changing the default order?