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