There is a regular operation in the sequence container , That's sort .
list.sort and built-in sorted function
list.sort The method is to sort the list in place , That is to say, no new list will be generated , It returns after execution None. This method returns None Indicates that it does not create a new list object and returns , And it changes the list in place , This programming habit is Python It's a tradition . So all Python Of API Functions or methods follow this preference , Clearly tell the caller , It changes the object in place , No new object creation returns . If you go and check , Will find random.shuffle The method also follows this rule .
contrary , Built in functions sorted Is to create a new list object to return , Because it doesn't just receive lists , You can also receive non modifiable sequence containers , In this way, a new object must be returned in order to be valid .list.sort and sorted Both receive two keyword parameters ：
reverse： Set to True Means to sort data in descending order , Otherwise set to False It's in ascending order , This is the default way .
key： A parameter function is used to calculate the key value of the sort . For example, when sorting a string , Set up key = str.lower Represents a case insensitive sort ; Set to key=len Represents the length of the string