# Aggregations with sorting

**URL:** <https://discuss.elastic.co/t/aggregations-with-sorting/49724>\
**Category:** Elasticsearch\
**Created:** [May 11, 2016, 6:00am UTC](https://discuss.elastic.co/t/aggregations-with-sorting/49724 "2016-05-11T06:00:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ravi\_Kumar\_N](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@Ravi\_Kumar\_N](https://discuss.elastic.co/u/Ravi_Kumar_N)\
**Post date:** [May 11, 2016, 6:00am UTC](https://discuss.elastic.co/t/aggregations-with-sorting/49724/1 "2016-05-11T06:00:08Z")

</div>

```
{
  "aggs": {
    "authors": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "most_recent_book": {
          "top_hits": {
            "sort": [
              {
                "last_publication_date": {
                  "order": "desc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}
Results return _source of all documents which has other fields like : title, co_author, isbn, genre, rating, etc…

```

A UI displays these records in a tabular form with column sorting enabled. How do I apply sort on say “rating” or “co\_author” for the above query.

---

<div class="post-metadata">

**Author:** ![Ravi\_Kumar\_N](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@Ravi\_Kumar\_N](https://discuss.elastic.co/u/Ravi_Kumar_N)\
**Post date:** [May 11, 2016, 4:49pm UTC](https://discuss.elastic.co/t/aggregations-with-sorting/49724/2 "2016-05-11T16:49:22Z")

</div>

In short the ask is :  
The user wants to display titles alphabetically, the latest book of all authors.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:52pm UTC](https://discuss.elastic.co/t/aggregations-with-sorting/49724/3 "2017-07-05T22:52:27Z")

</div>

Closed on July 5, 2017, 10:52pm UTC


