sort by specified fields (see fields()) Example:: a.sort(1, nums = True) Sorts a by second field, in numerical order (so that 21 > 3)
(self,field= None, nums = False)
| 201 | return res |
| 202 | |
| 203 | def sort(self,field= None, nums = False): |
| 204 | """ sort by specified fields (see fields()) |
| 205 | |
| 206 | Example:: |
| 207 | |
| 208 | a.sort(1, nums = True) |
| 209 | |
| 210 | Sorts a by second field, in numerical order (so that 21 > 3) |
| 211 | |
| 212 | """ |
| 213 | |
| 214 | #decorate, sort, undecorate |
| 215 | if field is not None: |
| 216 | dsu = [[SList([line]).fields(field), line] for line in self] |
| 217 | else: |
| 218 | dsu = [[line, line] for line in self] |
| 219 | if nums: |
| 220 | for i in range(len(dsu)): |
| 221 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
| 222 | try: |
| 223 | n = int(numstr) |
| 224 | except ValueError: |
| 225 | n = 0 |
| 226 | dsu[i][0] = n |
| 227 | |
| 228 | |
| 229 | dsu.sort() |
| 230 | return SList([t[1] for t in dsu]) |
| 231 | |
| 232 | |
| 233 | # FIXME: We need to reimplement type specific displayhook and then add this |