This article describes the python sorting method. Share it for your reference. The details are as follows:
>>> def my_key1(x): ... return x % 10 ... >>> alist = [4, 5, 8, 1, 63, 8] >>> alist [4, 5, 8, 1, 63, 8] >>> () # Default ascending order sort>>> alist [1, 4, 5, 8, 8, 63] >>> (reverse = True) # Sort in descending order>>> alist [63, 8, 8, 5, 4, 1] >>> (key = my_key1) # Set the sorted key value>>> alist [1, 63, 4, 5, 8, 8] >>> >>> def my_key2(x): ... return x[1] ... >>> alist = [(5,'a'),(1,'w'),(2,'e'),(6,'f')] >>> (key = my_key2) # Sort by the second component of each tuple>>> alist [(5, 'a'), (2, 'e'), (6, 'f'), (1, 'w')] >>>
I hope this article will be helpful to everyone's Python programming.