This class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or descending) for each tuple-index. The compare functions can then be used as the function
| 520 | print(func_std_string(func), file=self.stream) |
| 521 | |
| 522 | class TupleComp: |
| 523 | """This class provides a generic function for comparing any two tuples. |
| 524 | Each instance records a list of tuple-indices (from most significant |
| 525 | to least significant), and sort direction (ascending or descending) for |
| 526 | each tuple-index. The compare functions can then be used as the function |
| 527 | argument to the system sort() function when a list of tuples need to be |
| 528 | sorted in the instances order.""" |
| 529 | |
| 530 | def __init__(self, comp_select_list): |
| 531 | self.comp_select_list = comp_select_list |
| 532 | |
| 533 | def compare (self, left, right): |
| 534 | for index, direction in self.comp_select_list: |
| 535 | l = left[index] |
| 536 | r = right[index] |
| 537 | if l < r: |
| 538 | return -direction |
| 539 | if l > r: |
| 540 | return direction |
| 541 | return 0 |
| 542 | |
| 543 | |
| 544 | #************************************************************************** |