This question has been flagged
3 Replies
43579 Views

Hello everyone,

Based on the Odoo documentation for Odoo v12, there are 2 functions that helps me filter and sort lists.

1. filted() sample code:

records.filtered(lambda r: r.company_id == user.company_id)

The code above will allow me to filter records whose company is the current user's company

2. sorted() sample code:

records.sorted(key=lambda r: r.name)

The code above will allow me to sort records by name

My question is:

How can I filter and return a sorted list by choosing which field to sort from using filter() and sort() (or any other) function in Odoo?

Thank you all in advance

Best regards

Paulo


Avatar
Discard
Best Answer

Hi,

You can try like this,

records.filtered(lambda r: r.company_id == user.company_id).sorted(key=lambda r: r.name)


For more: https://docs.python.org/3/library/functions.html#sorted

Also see this for more understanding: Odoo ORM: RecordSet Operations - Sorted, Filtered & Mapped


Thanks

Avatar
Discard
Author

Thank you very much once again my friend.

Regards

Author Best Answer

Dear @Niyas Raphy,

As usual you saved my day :)

Best regards

Paulo

Avatar
Discard