# How to sort query result by sum of a field?

**URL:** <https://discuss.elastic.co/t/how-to-sort-query-result-by-sum-of-a-field/151056>\
**Category:** Elasticsearch\
**Created:** [October 4, 2018, 2:03pm UTC](https://discuss.elastic.co/t/how-to-sort-query-result-by-sum-of-a-field/151056 "2018-10-04T14:03:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_houshyaripour](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_houshyaripour/32/35931_2.png) [@ali\_houshyaripour](https://discuss.elastic.co/u/ali_houshyaripour)\
**Post date:** [October 4, 2018, 2:03pm UTC](https://discuss.elastic.co/t/how-to-sort-query-result-by-sum-of-a-field/151056/1 "2018-10-04T14:03:53Z")

</div>

Hi...  
I have an index with name "user\_activity" in elastic and a query that get all of result, now I want to sort my result from it by sum of coin fields of every user, this means that users should be sort by sum(coin) desc to find best three of them.

```auto
|-----------------------|
| id user_id coin |
|-----------------------|
| 1 1 1 |
| 2 2 5 |
| 3 1 3 |
| 4 2 5 |
| 5 3 2 |
| 6 4 6 |
| 7 5 10 |
| 8 5 10 |
| 9 5 25 |
| 10 1 3 |
|-----------------------|

```

Anyone can help me?

---

<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:** [November 1, 2018, 2:04pm UTC](https://discuss.elastic.co/t/how-to-sort-query-result-by-sum-of-a-field/151056/2 "2018-11-01T14:04:00Z")

</div>

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