.. index:: py::builtins::list::sort .. _doxycast_pypp_classpy_1_1builtins_1_1list_1a1fc099803619c4698b6067679b4e804d: .. _py-builtins-list-sort: *py::builtins::list::*\ sort ============================ .. code-block:: cpp template > void sort(bool reverse = false, compare_func_t key = nullptr); Removes the given element. Template parameters ------------------- ``compare_func_t``: The type of compare function (``key``). .. admonition:: Requirements of ``compare_func_t`` ``compare_func_t`` must support function call operator (``operator ()``). Parameters ---------- ``reverse``: Whether to reserve sort the list. ``key``: A function (can be a callable object or lambda) whose return value used as the key to sort. Complexity ---------- Linear in the size of the list multipled by the logarithm of the size of the list, plus linear in the size of the list if reverse sorted. Example ------- .. code-block:: cpp #include #include int main() { py::list list = {0, 1, 3, 0}; list.sort(); py::print(list); list.sort(true); // Reverse py::print(list); } **Output**: .. code-block:: text {0, 0, 1, 3} {3, 1, 0, 0}