# How to sort results with a term

**URL:** <https://discuss.elastic.co/t/how-to-sort-results-with-a-term/371250>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring\
**Created:** [November 30, 2024, 7:54am UTC](https://discuss.elastic.co/t/how-to-sort-results-with-a-term/371250 "2024-11-30T07:54:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [November 30, 2024, 7:54am UTC](https://discuss.elastic.co/t/how-to-sort-results-with-a-term/371250/1 "2024-11-30T07:54:29Z")

</div>

Hi everyone,

I'm interested in searching docs with term and sorting them in the order:

1. all docs should be sorted by field "date";

2. first should come results where the field "isPerpetual": true and then all other fields with sorting by "date" field. Docs where "isPerpetual": true should be sorted by date too. Then comes all other documents where "isPerpetual": false and this lost should be sorted by date field too.

I have docs in my index:

```auto
{
"id": 1,
"date": 2024-01-01,
"price": 20,
"isPerpetual": true
},
{
"id": 2,
date": 2024-02-01,
"price": 30,
"isPerpetual": false
},
{
"id": 3,
date": 2024-03-01,
"price": 40,
"isPerpetual": true
},
{
"id": 4,
date": 2024-02-02,
"price": 30,
"isPerpetual": false
},
{
"id": 5,
date": 2024-04-01,
"price": 30,
"isPerpetual": true
}

```

---

<div class="post-metadata">

**Author:** ![Tortoise](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tortoise/32/147587_2.png) [@Tortoise](https://discuss.elastic.co/u/Tortoise)\
**Post date:** [November 30, 2024, 9:36am UTC](https://discuss.elastic.co/t/how-to-sort-results-with-a-term/371250/2 "2024-11-30T09:36:27Z")

</div>

> [@orlenkoda5](#):
>
> - all docs should be sorted by field "date";
> - first should come results where the field "isPerpetual": true and then all other fields with sorting by "date" field. Docs where "isPerpetual": true should be sorted by date too. Then comes all other documents where "isPerpetual": false and this lost should be sorted by date field too.

Hello,

Could you please try below query & confirm if this is the way you need your data ?

```auto
{
  "query": {
    "match_all": {}
  },
  "sort": [
    { "isPerpetual": { "order": "desc" } },
    { "date": { "order": "asc" } }
  ]
}

```

Thanks!!
