# \-How to sort within nested objects

**URL:** <https://discuss.elastic.co/t/how-to-sort-within-nested-objects/220095>\
**Category:** Elasticsearch\
**Created:** [February 20, 2020, 6:13am UTC](https://discuss.elastic.co/t/how-to-sort-within-nested-objects/220095 "2020-02-20T06:13:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shafath\_Ahmed](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shafath_ahmed/32/61903_2.png) [@Shafath\_Ahmed](https://discuss.elastic.co/u/Shafath_Ahmed)\
**Post date:** [February 20, 2020, 6:13am UTC](https://discuss.elastic.co/t/how-to-sort-within-nested-objects/220095/1 "2020-02-20T06:13:33Z")

</div>

Hi All

I have a document like the following:

```
{
  "applicant" : {
      "firstName" : "tasd",
      "lastName" : "asdfasdf"
  },
  "employmentsHistory" : [
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2014-02-26",
      "endDate" : "2016-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2018-02-26",
      "endDate" : "2020-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2016-02-16",
      "endDate" : "2018-01-18"
    }
  ]     
}

```

I want to get the doc. The nested object **employmentsHistory** should be sorted on the **endDate** field when returned. i.e

Should return:

```
{
  "applicant" : {
      "firstName" : "tasd",
      "lastName" : "asdfasdf"
  },
  "employmentsHistory" : [
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2014-02-26",
      "endDate" : "2016-01-20"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2016-02-16",
      "endDate" : "2018-01-18"
    },
    {
      "companyName" : "asdfa 1",
      "isCurrentlyWorking" : false,
      "startDate" : "2018-02-26",
      "endDate" : "2020-01-20"
    }
  ]     
}

```

I have tried multiple approaches for sorting. Seems, those sort gets applied on the parent documents. The inner hits query approach returns data on separate scope also. Will be glad if anyone helps me out? Thanks.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 20, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-sort-within-nested-objects/220095/2 "2020-02-20T08:01:25Z")

</div>

can you be more exact what you mean with

> I want to get the doc

Are you searching for it? Are you just retrieving it? How does your search look like, can you provide an example? Also, please provide the mapping of your index plus index creation and full sample documents as part of a **reproducible but minimal** example, so others can reproduce your problem and understand more easily.

---

<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:** [March 19, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-sort-within-nested-objects/220095/3 "2020-03-19T08:01:25Z")

</div>

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