Odoo 17 Development Book - Sorting records

Sorting records

The sorted() function aids in sorting a recordset based on a given column, similar to the list sorting method in Python. Its first parameter accepts an iterable, such as a lambda function. The second parameter is a comparison key used during iteration (e.g., str(l.name).lower()). The third parameter, a boolean value, determines whether the sorting order should be reversed.


     partners = self.env['res.partner'].search([])
     partners.sorted(lambda o: o.create_date, reverse=True)
                             

Now, the partners variable contains the records of partners sorted by their create_date field, either in ascending or descending order, depending on your preference.

whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message