# How to sort on second same (field) value

**URL:** <https://discuss.elastic.co/t/how-to-sort-on-second-same-field-value/292888>\
**Category:** Elasticsearch\
**Created:** [December 24, 2021, 11:57am UTC](https://discuss.elastic.co/t/how-to-sort-on-second-same-field-value/292888 "2021-12-24T11:57:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wilco\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wilco_r/32/99514_2.png) [@Wilco\_R](https://discuss.elastic.co/u/Wilco_R)\
**Post date:** [December 24, 2021, 11:57am UTC](https://discuss.elastic.co/t/how-to-sort-on-second-same-field-value/292888/1 "2021-12-24T11:57:50Z")

</div>

Hi people,  
I ran into an sorting problem, which is very hard for me to find out how to do it.

What i want to achieve is to query the data to sort it in a way that the second(or higher) of the same configuration\_id is at the end of the results.  
Is there a way to to this? I assume it could be done with some aggregations, but i don't ahve any idea where to start with this.  
Could you guys help me to figure this out?

This is the data:

```auto
{
    "_index": "products",
    "_source": {
        "name": "Product 1",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 2",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 3",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 4",
        "configuration_id": "2"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 5",
        "configuration_id": "2"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product ",
        "configuration_id": "3"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product7",
        "configuration_id": "3"
    }
}

```

This is an example on how i would like to get the data in the result:

```auto
{
    "_index": "products",
    "_source": {
        "name": "Product 1",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 4",
        "configuration_id": "2"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product ",
        "configuration_id": "3"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 2",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 3",
        "configuration_id": "1"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product 5",
        "configuration_id": "2"
    }
},
{
    "_index": "products",
    "_source": {
        "name": "Product7",
        "configuration_id": "3"
    }
}

```

---

<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:** [January 21, 2022, 11:58am UTC](https://discuss.elastic.co/t/how-to-sort-on-second-same-field-value/292888/2 "2022-01-21T11:58:31Z")

</div>

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