# Sort aggregation by string

**URL:** <https://discuss.elastic.co/t/sort-aggregation-by-string/270579>\
**Category:** Elasticsearch\
**Created:** [April 19, 2021, 12:44pm UTC](https://discuss.elastic.co/t/sort-aggregation-by-string/270579 "2021-04-19T12:44:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [April 19, 2021, 12:44pm UTC](https://discuss.elastic.co/t/sort-aggregation-by-string/270579/1 "2021-04-19T12:44:51Z")

</div>

Hi all and sorry for my poor english.  
I'd like to sort by creation (string) field this query

```auto
GET /profiles/_search
{
    "aggs": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
                {  
                  "range": {
                  "creation": {
                    "from": "2017-03-11 T 00:00:00.000+0000",
                    "to": "2021-03-12 T 00:00:00.000+0000"}        
                  }
                } ,
                {
                  "terms":
                  {
                    "type" : ["default"]
                  }
                }
          ]          
        }        
      },
      "aggs": {
        "type_profile": {
          "terms": {
            "field": "type" 
          },
          "aggs":{
              "date_creation": {
                  "terms": {
                    "field": "creation"   
                  }  
                
              }
          }
          
        } 
      }      
    }     
  } ,
  "size": 0 
}

```

I tried using bucket\_sort but with no success  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 19, 2021, 3:27pm UTC](https://discuss.elastic.co/t/sort-aggregation-by-string/270579/2 "2021-04-19T15:27:30Z")

</div>

What do you want to sort? The hits? Or the aggregations?

---

<div class="post-metadata">

**Author:** ![Giovanni\_Di\_Lembo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giovanni_di_lembo/32/45714_2.png) [@Giovanni\_Di\_Lembo](https://discuss.elastic.co/u/Giovanni_Di_Lembo)\
**Post date:** [April 20, 2021, 7:06am UTC](https://discuss.elastic.co/t/sort-aggregation-by-string/270579/3 "2021-04-20T07:06:59Z")

</div>

I found solution using  
"order": { "\_key" : "desc" }  
in the deepest terms aggs

---

<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:** [May 18, 2021, 7:07am UTC](https://discuss.elastic.co/t/sort-aggregation-by-string/270579/4 "2021-05-18T07:07:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
