# Create sort\_ function for functional field

**URL:** https://discuss.tryton.org/t/create-sort-function-for-functional-field/1073
**Category:** Developer
**Created:** [January 24, 2019, 5:44pm UTC](https://discuss.tryton.org/t/create-sort-function-for-functional-field/1073 "2019-01-24T17:44:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)
#### Post date: [January 24, 2019, 5:44pm UTC](https://discuss.tryton.org/t/create-sort-function-for-functional-field/1073/1 "2019-01-24T17:44:06Z")

</div>

Hi,

I was reading the docs about functions to order funtional fields [6], [7] and reviewing some examples on tryton git [0], [1], [2], [3], [4], [5] but I could not find and example on how to use for a specific case that have.

Basically I have a model to record the gathering moves of every month and a group. Every group have more than 5 people, it will vary:

Moves:

Group, Month, Date,

Moves Lines:

Move, Party, Amount

I added a custom field that calculate on Party that get the sum of the move lines and it works fine, but I wish to order from highest to lowest.

Where could I find an example of how can order these type of field?

Thanks in advance!

[0] [party/party.py at develop · tryton/party · GitHub](https://github.com/tryton/party/blob/develop/party.py#L100)  
[1] [account/move.py at f8e616ba2aa40502e2f478c8506805adeee0f424 · tryton/account · GitHub](https://github.com/tryton/account/blob/f8e616ba2aa40502e2f478c8506805adeee0f424/move.py#L143)  
[2] [product/product.py at develop · tryton/product · GitHub](https://github.com/tryton/product/blob/develop/product.py#L279)  
[3] [purchase\_request\_quotation/purchase.py at 5d151e6896a0a1374f3b4f428425aab0515024de · tryton/purchase\_request\_quotation · GitHub](https://github.com/tryton/purchase_request_quotation/blob/5d151e6896a0a1374f3b4f428425aab0515024de/purchase.py#L320)  
[4] [timesheet/line.py at develop · tryton/timesheet · GitHub](https://github.com/tryton/timesheet/blob/develop/line.py#L354)  
[5] [party/address.py at b184b5996b7e983e0ce4beb75216962c532ef3b4 · tryton/party · GitHub](https://github.com/tryton/party/blob/b184b5996b7e983e0ce4beb75216962c532ef3b4/address.py#L255)  
[6] [http://docs.tryton.org/projects/server/en/latest/ref/models/fields.html?highlight=ordering#ordering](http://docs.tryton.org/projects/server/en/latest/ref/models/fields.html?highlight=ordering#ordering)  
[7] [http://docs.tryton.org/projects/server/en/latest/ref/models/fields.html?highlight=ordering#trytond.model.fields.Field.convert\_domain](http://docs.tryton.org/projects/server/en/latest/ref/models/fields.html?highlight=ordering#trytond.model.fields.Field.convert_domain)

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [January 25, 2019, 2:24pm UTC](https://discuss.tryton.org/t/create-sort-function-for-functional-field/1073/2 "2019-01-25T14:24:56Z")

</div>

> [@iehoshia](#):
>
> Where could I find an example of how can order these tye of field?

There is no such example because it would be resource consuming.

Any way, you have to make a join (adding to `tables` argument) with a query the will return the party id and the `SUM`. Then you return the clause to order with the `SUM` column of your query.

---

<div class="post-metadata">

### Author: ![JCavallo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JCavallo](https://discuss.tryton.org/u/JCavallo)
#### Post date: [February 15, 2019, 11:20am UTC](https://discuss.tryton.org/t/create-sort-function-for-functional-field/1073/3 "2019-02-15T11:20:53Z")

</div>

I just updated the training module with an example of an “order” method implementation:

> **[5.0 step10](https://github.com/coopengo/tryton-training/wiki/5.0-step10)**
>
> Contribute to coopengo/tryton-training development by creating an account on GitHub.

Search for “Defining a order method”

Edit : For your use case, you would have to add an entry in the “tables” dict which is a full sub-query.
