Skip to content

comicbox.readinglists.combine.order

Put the groups of several lists in one reading order.

The tokens are group ids. The first ordered input is the backbone, and nothing ever moves one of its entries: further ordered inputs only insert the groups the merged sequence lacks, unordered inputs place theirs by date, and with no ordered input everything sorts by date.

GroupOrderConflict dataclass

A shared group an input puts elsewhere; group is a group id.

Source code in comicbox/readinglists/combine/order.py
20
21
22
23
24
25
26
@dataclass(frozen=True, slots=True)
class GroupOrderConflict:
    """A shared group an input puts elsewhere; ``group`` is a group id."""

    group: int
    input_index: int
    row: int

order_groups(sequences, ordered, sort_keys, dates)

Order every group, given each input’s sequence of groups in rank order.

sort_keys and dates are indexed by group id. Returns the group ids in reading order and the order conflicts.

Source code in comicbox/readinglists/combine/order.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def order_groups(
    sequences: Sequence[Sequence[int]],
    ordered: Sequence[bool],
    sort_keys: Sequence[tuple[Any, ...]],
    dates: Sequence[date | None],
) -> tuple[list[int], list[GroupOrderConflict]]:
    """
    Order every group, given each input's sequence of groups in rank order.

    ``sort_keys`` and ``dates`` are indexed by group id. Returns the group
    ids in reading order and the order conflicts.
    """
    ordered_inputs = [index for index, flag in enumerate(ordered) if flag]
    if not ordered_inputs:
        # Group ids number groups by first appearance, the last tiebreak.
        return sorted(range(len(sort_keys)), key=lambda g: (sort_keys[g], g)), []
    merged = list(sequences[ordered_inputs[0]])
    conflicts: list[GroupOrderConflict] = []
    for input_index in ordered_inputs[1:]:
        insert = _OrderedInsert(merged, sequences[input_index], input_index)
        merged = insert.run()
        conflicts.extend(insert.conflicts)
    for input_index, flag in enumerate(ordered):
        if not flag:
            merged = _insert_unordered(merged, sequences[input_index], sort_keys, dates)
    return merged, conflicts