# Sort one field wrt another

**URL:** <https://discuss.elastic.co/t/sort-one-field-wrt-another/25984>\
**Category:** Elasticsearch\
**Created:** [July 21, 2015, 12:16pm UTC](https://discuss.elastic.co/t/sort-one-field-wrt-another/25984 "2015-07-21T12:16:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 21, 2015, 12:16pm UTC](https://discuss.elastic.co/t/sort-one-field-wrt-another/25984/1 "2015-07-21T12:16:46Z")

</div>

Hi  
How would i sort my search based on one field in the id's, I tried setting up using sort command and output just id's but it was not working. Basically i want top 4 id's based on one particular metric

GET /ind/table/\_search/  
{  
"sort": [  
{  
"val": {"order" : "asc"}  
}  
],  
"query": {  
"term": { "id" }  
},  
}

Do we have to use aggregation, can i get example based on it?

Thanks again

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 21, 2015, 5:37pm UTC](https://discuss.elastic.co/t/sort-one-field-wrt-another/25984/2 "2015-07-21T17:37:21Z")

</div>

Can you be more specific about what is not working?

---

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [July 22, 2015, 2:56am UTC](https://discuss.elastic.co/t/sort-one-field-wrt-another/25984/3 "2015-07-22T02:56:20Z")

</div>

Thanks for the reply.  
I think my current query is not the correct way to go about it. My final objective is  
Suppose we have two fields in our objects: val and user. Both are not unique. I wanted to take average of val for each unique user and sort them out.  
ex. user: 23, val : 1  
user: 24, val: 4  
user: 23, val: 8  
In output:  
user 23 ------ 4.5 (Average)  
user 24 ------ 4 (Average)

The following can be done in kibana using metric table.

---

<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 6, 2017, 12:00am UTC](https://discuss.elastic.co/t/sort-one-field-wrt-another/25984/4 "2017-07-06T00:00:04Z")

</div>


