Assuming you want the default sort order, you can use sorted (list) or list.sort (). If you need your own sorting logic, Python lists support the ability to sort based on the function you are passing in. For example, the following will be a way to sort numbers from smallest to largest (default behavior) using a function.
def compareTwo(a, b): if a > b: return 1 if a == b: return 0 if a < b: return -1 List.Sort(compareTwo) print a
This approach is conceptually a little cleaner than manually, creating a new list and adding new values ββand allowing you to control the sorting logic.
source share