Design Pattern Examples
Overview of object-oriented design patterns
strategy_sortentries_class.py
Go to the documentation of this file.
6
7from .strategy_isortentries import ISortEntries, SortOptions
8from .strategy_entryinformation import EntryInformation
9
10
11
13
14 # <summary>
15 # Constructor.
16 # </summary>
17 # @param reversedSort">true if to sort in descending order; otherwise,
18 # sort in ascending order.
19 def __init__(self, reversedSort : bool) -> None:
20 self._reversedSort = reversedSort
21
22
24
25
26
36 def Sort(self, entries : list[EntryInformation]) -> None:
37 entries.sort(reverse=self._reversedSort, key=lambda entry : entry.Name)
38
39
40
42 def ToString(self) -> str:
43 return "Strategy_SortEntries_ByName"
44
45
46
48
49
50
52
53
58 def __init__(self, reversedSort : bool) -> None:
59 self._reversedSort = reversedSort
60
61
63
64
65
75 def Sort(self, entries : list[EntryInformation]) -> None:
76 entries.sort(reverse=self._reversedSort, key=lambda entry : entry.Age)
77
78
79
81 def ToString(self) -> str:
82 return "Strategy_SortEntries_ByAge"
83
84
85
87
88
89
91
92
97 def __init__(self, reversedSort : bool) -> None:
98 self._reversedSort = reversedSort
99
100
102
103
104
114 def Sort(self, entries : list[EntryInformation]) -> None:
115 entries.sort(reverse=self._reversedSort, key=lambda entry : entry.Height)
116
117
118
120 def ToString(self) -> str:
121 return "Strategy_SortEntries_ByHeight"
122
123
124
126
127
137 def _SortOptionToString(sortOption : SortOptions) -> str:
138 optionAsString = ""
139
140 match sortOption:
141 case SortOptions.ByName:
142 optionAsString = "ByName"
143
144 case SortOptions.ByAge:
145 optionAsString = "ByAge"
146
147 case SortOptions.ByHeight:
148 optionAsString = "ByHeight"
149
150 case _:
151 optionAsString = "Unknown {0}".format(int(sortOption))
152
153 return optionAsString
154
155
156
171 def Create(sortOption : SortOptions, reversedSort : bool) -> ISortEntries:
172 sortEntries = None # type: ISortEntries
173
174 match sortOption:
175 case SortOptions.ByName:
176 sortEntries = Strategy_SortEntries_ByName(reversedSort)
177
178 case SortOptions.ByAge:
179 sortEntries = Strategy_SortEntries_ByAge(reversedSort)
180
181 case SortOptions.ByHeight:
182 sortEntries = Strategy_SortEntries_ByHeight(reversedSort)
183
184 case _:
185 message = "Unrecognized sort option: {0}".format(Strategy_SortEntries_ClassFactory._SortOptionToString(sortOption))
186 raise ValueError(message)
187
188 return sortEntries
None Sort(self, list[EntryInformation] entries)
Sort the specified list of entries in place.
None Sort(self, list[EntryInformation] entries)
Sort the specified list of entries in place.
None Sort(self, list[EntryInformation] entries)
Sort the specified list of entries in place.
ISortEntries Create(SortOptions sortOption, bool reversedSort)
Generate an instance of a sorting strategy based on the given sorting option and reversed sort flag.
str _SortOptionToString(SortOptions sortOption)
Convert a SortOptions enumeration to a string.