# How can I sort the topic replies by date

**URL:** https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344
**Category:** Migration
**Tags:** rest-api
**Created:** [September 7, 2022, 2:18pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344 "2022-09-07T14:18:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [September 7, 2022, 2:18pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/1 "2022-09-07T14:18:00Z")

</div>

Hello. I don’t know if it’s possible, and if it is I googled a lot and did not find anything about this, but I would like to sort all the replies of all the topics of my forum by the created date.

I’ve been working on a migration of data from an old forum to a brand new Discourse one. To do that, I am recreating the topics and replies via API so that I can send them to Discourse on the date the reply was created initially. That has been working just fine as when I go to the created replies on Discourse I can see that the original date is over there.

 ![image](https://global.discourse-cdn.com/meta/original/4X/f/6/3/f636e8b7869b16e8a9edac1c213929438febc062.jpeg)

But even though all the replies are with the original date, they are sorted by the order they were created on the API. I don’t know if this is a bug or meant to be or if there is any way to sort them by the date I set on the API POST call, but if so I’d like some help on this topic. Cheers guys.

I am using version 2.8.8.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 7, 2022, 3:12pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/2 "2022-09-07T15:12:39Z")

</div>

> [@CarlosDaniel](#):
>
> To do that, I am recreating the topics and replies via API

Don’t do that. You want to use an import script. You can look at [discourse/script/import\_scripts at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/tree/main/script/import_scripts) for examples and search here for instructions for running import scripts for other forums.

Posts are displayed in the order in which they are created. So you’d need to have your API script (that you really don’t want to use) create them in the proper order.

---

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [September 8, 2022, 10:05am UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/3 "2022-09-08T10:05:58Z")

</div>

Hmmm, thanks for the reply. I did think about using a script a while ago but since Discourse has a lot of table relationships I decided to go through the API. But is there any way to create a script that will insert data in one table and then Discourse handles the rest of the relationship? That might make things easier for me.

---

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [September 8, 2022, 10:13am UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/4 "2022-09-08T10:13:14Z")

</div>

But I mean, the migration via API has been working so far. I wonder what is the point of being able to send a `created_at` attribute on the [endpoint](https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM) to create posts if Discourse won’t show it sorted by it anyway… Shouldn’t this be a feature request?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 8, 2022, 10:42am UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/5 "2022-09-08T10:42:46Z")

</div>

> [@CarlosDaniel](#):
>
> But is there any way to create a script that will insert data in one table and then Discourse handles the rest of the relationship

Yes, in fact, there is! I linked to examples.

> [@CarlosDaniel](#):
>
> the migration via API has been working so far.

If that were true, you’d not have posted. 😉

> [@CarlosDaniel](#):
>
> Shouldn’t this be a feature request

No, because the way to solve your problem is to use one of the dozens of examples that I linked to is the way to solve your problem. I promise it will be much, much easier. I wrote several import scripts before I really even learned ruby.

Even if you wanted to use the API, you still need to create the posts in the `created_at` order. There are ways to sort them in a plugin v(or maybe theme component?) , but you really don’t want to do that.

@JammyDodger points out that there is a rake task

```
    rake posts:reorder_posts

```

That might solve your problem. If you are importing a few dozens of topics and a few hundreds of posts, you might give that a try.

---

<div class="post-metadata">

### Author: ![CarlosDaniel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlosdaniel/32/273223_2.png) [@CarlosDaniel](https://meta.discourse.org/u/CarlosDaniel)
#### Post date: [September 8, 2022, 12:37pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/6 "2022-09-08T12:37:36Z")

</div>

I see. I will keep following my idea, just wanted to know regarding the `created_at` attribute on the POST call when creating a post - which is, as it seems, useless, if the posts can’t be sorted by it. Thanks anyway.

---

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [September 8, 2022, 12:52pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/7 "2022-09-08T12:52:37Z")

</div>

Just FYI, the reason things don’t just sort by created date is because things are designed to support moving posts from one topic to another, and in that scenario, it’s proven less desireable to have posts get “mixed up” if the dates overlap.

Here’s an earlier discussion about that:

> [@Moved posts to existing topic - dates out of order](https://meta.discourse.org/t/moved-posts-to-existing-topic-dates-out-of-order/83980/1):
>
> Not sure if this is the correct behaviour, but when moving posts to an existing topic do the posts “blend” together by date or do all the posts that are “merged in” appear at the bottom and dates are ignored.

On the topic of how best to approach a migration, I agree w/ Jay. You’re likely better off following prior art here. While not a web API, the existing scripts rely on a lower level “migration API” of sorts with this in mind.

The web API is largely designed as an API for the front-end Discourse app. While it’s successfully used for various integrations, it is optimized for it’s primary purpose.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [September 8, 2022, 5:52pm UTC](https://meta.discourse.org/t/how-can-i-sort-the-topic-replies-by-date/238344/8 "2022-09-08T17:52:37Z")

</div>

> [@CarlosDaniel](#):
>
> as it seems, useless,

It’s not useless, it sets the `created_at` date as advertised. Just sort your posts by `created_at` before you create them.
