# sorted# Sort Algorithm # sort is also the algorithm that is often used in the program # regardless of whether you use a bubbling sort or a quick sort, the core of the sort is to compare the size of the two elements # if it's a number, we can compare it directly, but if it's a string or two dict, it doesn't make sense to compare the math size directly, Therefore, the process of comparison must be abstracted out by function # Python's built-in sorted () function to sort the list L = sorted ([5, -12, 9, -21]) print (l) # In addition, the sorted () function is also a higher-order function, It can also receive a key function to implement a custom sort # sorted by absolute size L = sorted ([5, -12, 9, -21], key=abs) print (l) # Key The function specified will be applied to each element of the list, Sorted by the result returned by the key function # string Sort L = sorted ([' Bob ', ' about ', ' Zoo ', ' credits ']) print (l) # By default, the string is sorted by the size of ASCII, because ' Z ' < ' A ', the result capital letter Z will be ranked in the front of lowercase a # string ignoring case, alphabetical order L = sorted ([' Bob ', ' about ', ' Zoo ', ' credits '], Key=str.lower) print (l) # reverse Sort L = Sorted ([' Bob ', ' about ', ' Zoo ', ' credits '], Key=str.lower, reverse=true) print (L) # High-order functions are powerful in abstraction, and the core code can be kept concise
Python---Sorted