sort by specified fields (see fields()) Example:: a.sort(1, nums = True) Sorts a by second field, in numerical order (so that 21 > 3)
( # type:ignore[override]
self,
field: Optional[List[str]] = None,
nums: bool = False,
)
| 222 | return res |
| 223 | |
| 224 | def sort( # type:ignore[override] |
| 225 | self, |
| 226 | field: Optional[List[str]] = None, |
| 227 | nums: bool = False, |
| 228 | ) -> Self: |
| 229 | """sort by specified fields (see fields()) |
| 230 | |
| 231 | Example:: |
| 232 | |
| 233 | a.sort(1, nums = True) |
| 234 | |
| 235 | Sorts a by second field, in numerical order (so that 21 > 3) |
| 236 | |
| 237 | """ |
| 238 | |
| 239 | #decorate, sort, undecorate |
| 240 | if field is not None: |
| 241 | dsu = [[SList([line]).fields(field), line] for line in self] |
| 242 | else: |
| 243 | dsu = [[line, line] for line in self] |
| 244 | if nums: |
| 245 | for i in range(len(dsu)): |
| 246 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
| 247 | try: |
| 248 | n = int(numstr) |
| 249 | except ValueError: |
| 250 | n = 0 |
| 251 | dsu[i][0] = n |
| 252 | |
| 253 | |
| 254 | dsu.sort() |
| 255 | return type(self)([t[1] for t in dsu]) |
| 256 | |
| 257 | |
| 258 | def indent(instr: str, nspaces: int = 4, ntabs: int = 0, flatten: bool = False) -> str: |