@@ -4,6 +4,7 @@ Sorting HOW TO
44**************
55
66:Author: Andrew Dalke and Raymond Hettinger
7+ :Release: 0.1
78
89
910Python lists have a built-in :meth: `list.sort ` method that modifies the list
@@ -17,7 +18,7 @@ Sorting Basics
1718==============
1819
1920A simple ascending sort is very easy: just call the :func: `sorted ` function. It
20- returns a new sorted list:
21+ returns a new sorted list::
2122
2223 >>> sorted([5, 2, 3, 1, 4])
2324 [1, 2, 3, 4, 5]
@@ -57,28 +58,28 @@ A common pattern is to sort complex objects using some of the object's indices
5758as keys. For example:
5859
5960 >>> student_tuples = [
60- ... (' john' , ' A' , 15 ),
61- ... (' jane' , ' B' , 12 ),
62- ... (' dave' , ' B' , 10 ),
63- ... ]
61+ ('john', 'A', 15),
62+ ('jane', 'B', 12),
63+ ('dave', 'B', 10),
64+ ]
6465 >>> sorted (student_tuples, key = lambda student : student[2 ]) # sort by age
6566 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
6667
6768The same technique works for objects with named attributes. For example:
6869
6970 >>> class Student :
70- ... def __init__ (self , name , grade , age ):
71- ... self .name = name
72- ... self .grade = grade
73- ... self .age = age
74- ... def __repr__ (self ):
75- ... return repr ((self .name, self .grade, self .age))
71+ def __init__(self, name, grade, age):
72+ self.name = name
73+ self.grade = grade
74+ self.age = age
75+ def __repr__(self):
76+ return repr((self.name, self.grade, self.age))
7677
7778 >>> student_objects = [
78- ... Student(' john' , ' A' , 15 ),
79- ... Student(' jane' , ' B' , 12 ),
80- ... Student(' dave' , ' B' , 10 ),
81- ... ]
79+ Student('john', 'A', 15),
80+ Student('jane', 'B', 12),
81+ Student('dave', 'B', 10),
82+ ]
8283 >>> sorted (student_objects, key = lambda student : student.age) # sort by age
8384 [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
8485
@@ -207,39 +208,39 @@ return a negative value for less-than, return zero if they are equal, or return
207208a positive value for greater-than. For example, we can do:
208209
209210 >>> def numeric_compare (x , y ):
210- ... return x - y
211- >>> sorted ([5 , 2 , 4 , 1 , 3 ], cmp = numeric_compare) # doctest: +SKIP
211+ return x - y
212+ >>> sorted ([5 , 2 , 4 , 1 , 3 ], cmp = numeric_compare)
212213 [1, 2, 3, 4, 5]
213214
214215Or you can reverse the order of comparison with:
215216
216217 >>> def reverse_numeric (x , y ):
217- ... return y - x
218- >>> sorted ([5 , 2 , 4 , 1 , 3 ], cmp = reverse_numeric) # doctest: +SKIP
218+ return y - x
219+ >>> sorted ([5 , 2 , 4 , 1 , 3 ], cmp = reverse_numeric)
219220 [5, 4, 3, 2, 1]
220221
221222When porting code from Python 2.x to 3.x, the situation can arise when you have
222223the user supplying a comparison function and you need to convert that to a key
223- function. The following wrapper makes that easy to do:
224-
225- >>> def cmp_to_key (mycmp ):
226- ... ' Convert a cmp= function into a key= function'
227- ... class K (object ):
228- ... def __init__ (self , obj , * args ):
229- ... self .obj = obj
230- ... def __lt__ (self , other ):
231- ... return mycmp(self .obj, other.obj) < 0
232- ... def __gt__ (self , other ):
233- ... return mycmp(self .obj, other.obj) > 0
234- ... def __eq__ (self , other ):
235- ... return mycmp(self .obj, other.obj) == 0
236- ... def __le__ (self , other ):
237- ... return mycmp(self .obj, other.obj) <= 0
238- ... def __ge__ (self , other ):
239- ... return mycmp(self .obj, other.obj) >= 0
240- ... def __ne__ (self , other ):
241- ... return mycmp(self .obj, other.obj) != 0
242- ... return K
224+ function. The following wrapper makes that easy to do::
225+
226+ def cmp_to_key(mycmp):
227+ 'Convert a cmp= function into a key= function'
228+ class K(object):
229+ def __init__(self, obj, *args):
230+ self.obj = obj
231+ def __lt__(self, other):
232+ return mycmp(self.obj, other.obj) < 0
233+ def __gt__(self, other):
234+ return mycmp(self.obj, other.obj) > 0
235+ def __eq__(self, other):
236+ return mycmp(self.obj, other.obj) == 0
237+ def __le__(self, other):
238+ return mycmp(self.obj, other.obj) <= 0
239+ def __ge__(self, other):
240+ return mycmp(self.obj, other.obj) >= 0
241+ def __ne__(self, other):
242+ return mycmp(self.obj, other.obj) != 0
243+ return K
243244
244245To convert to a key function, just wrap the old comparison function:
245246
0 commit comments