# Sorting on Nested array object within the same json file

**URL:** <https://discuss.elastic.co/t/sorting-on-nested-array-object-within-the-same-json-file/275767>\
**Category:** Elasticsearch\
**Created:** [June 13, 2021, 3:32pm UTC](https://discuss.elastic.co/t/sorting-on-nested-array-object-within-the-same-json-file/275767 "2021-06-13T15:32:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhan88](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@madhan88](https://discuss.elastic.co/u/madhan88)\
**Post date:** [June 13, 2021, 3:32pm UTC](https://discuss.elastic.co/t/sorting-on-nested-array-object-within-the-same-json-file/275767/1 "2021-06-13T15:32:32Z")

</div>

Hi,

I'm trying to sort the fields present within an array which is of type nested.

My data :

{  
"name" : "abc",  
"shipping":[  
{  
"shipmentcode" : 5,  
"shipmentstatus" : "resolved",  
"shipmentorderid": "12345"  
},  
{  
"shipmentcode" : 4,  
"shipmentstatus" : "delivered",  
"shipmentorderid": "12345"  
},  
{  
"shipmentcode" : 1,  
"shipmentstatus" : "orderconfirmed",  
"shipmentorderid": "12345"  
},  
{  
"shipmentcode" : 2,  
"shipmentstatus" : "RTF",  
"shipmentorderid": "12345"  
},  
{  
"shipmentcode" : 3,  
"shipmentstatus" : "shipped","shipmentorderid": "12345"  
}  
]  
}

In here, I have to run a query a term query to find the docs that matches name to "abc" and in that result I have to sort the shipping array based on the "shipmentcode"..something like the below when sorter in desc order-

"shipping" : [  
{  
"shipmentcode" : 5,  
"shipmentstatus" : "resolved",  
"shipmentorderid" : "12345"  
},  
{  
"shipmentcode" : 4,  
"shipmentstatus" : "delivered",  
"shipmentorderid" : "12345"  
},  
{  
"shipmentcode" : 3,  
"shipmentstatus" : "shipped",  
"shipmentorderid" : "12345"  
},  
{  
"shipmentcode" : 2,  
"shipmentstatus" : "RTF",  
"shipmentorderid" : "12345"  
},  
{  
"shipmentcode" : 1,  
"shipmentstatus" : "orderconfirmed",  
"shipmentorderid" : "12345"  
}  
]  
Is this even possible in elasticsearch? I have to sort on the result!!

---

<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:** [June 13, 2021, 7:03pm UTC](https://discuss.elastic.co/t/sorting-on-nested-array-object-within-the-same-json-file/275767/2 "2021-06-13T19:03:47Z")

</div>

If it is one single document, it is shown exactly as you sent it. No internal sorting of arrays then.

But if you index instead every shipping as a single document, then that'd be possible.

---

<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 11, 2021, 7:04pm UTC](https://discuss.elastic.co/t/sorting-on-nested-array-object-within-the-same-json-file/275767/3 "2021-07-11T19:04:16Z")

</div>

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