1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{sort [FUNCTION]}
19
20 Input objects are sorted before being written to output. If
21 C{FUNCTION} is not provided then input objects are sorted using the
22 default ordering (i.e., based on the C{cmp} function).
23
24 If C{FUNCTION} is provided, then sorting is done by applying C{cmp} to
25 values obtained by applying C{FUNCTION} to the input objects.
26 """
27
28 import osh.core
29
30
33
34
35 -def sort(function = None):
36 """Input objects are sorted before being written to output. If
37 C{function} is not provided then input objects are sorted using the
38 default ordering (i.e., based on the C{cmp} function).
39 If C{function} is provided, then sorting is done by applying C{cmp} to
40 values obtained by applying C{function} to the input objects.
41 """
42 args = []
43 if function:
44 args.append(function)
45 return _Sort().process_args(*args)
46
48
49 _key = None
50 _list = None
51
52
53
54
57
58
59
60
63
65 args = self.args()
66 if args.has_next():
67 self._key = args.next_function()
68 if args.has_next():
69 self.usage()
70 self._list = []
71
73 self._list.append(object)
74
76 key = self._key
77 if key:
78
79 self._list = [(key(*object), object) for object in self._list]
80 self._list.sort(lambda x, y: cmp(x[0], y[0]))
81 self._list = [t[1] for t in self._list]
82 else:
83 self._list.sort()
84 for object in self._list:
85 self.send(object)
86 self.send_complete()
87