<a id="ch-12"></a>

## Chapter 12 — A tie uses the id, and F2 still finishes last

<!-- open-cyber-academy:lesson-figures:start ch-12 -->
### Lesson map

Lesson 12. Map, example, EX058.

```mermaid
flowchart TD
  P["F1 and F3 share product 6"] --> I["Second key compares F1 and F3"]
  I --> A["F1 prints before F3"]
  A --> Z["F2 remains the last line"]
```

Equal products do not leave the original list order in charge. The id is in the key.

### Logical drawing

```text
product 6
  "F1" then "F3"
product 4
  F4
product 2
  F2 last
```
<!-- open-cyber-academy:lesson-figures:end ch-12 -->

### Learning outcomes

After this chapter, you should be able to:

1. Say why F1 prints before F3.
2. Say where F2 sat in the original list.
3. Refuse a claim that the print kept written order.

### 12.1 `"F1" < "F3"`. That comparison is the tie. F2 is a different product.

F1 and F3 both produce 6. Their keys are `(-6, "F1")` and `(-6, "F3")`. The first components match, so `sorted` compares the strings. `"F1"` is less than `"F3"`, so F1 comes first. This list has no `F10`. A longer id would be a different string comparison, and this file does not contain one.

F2 is the second tuple in the source and the last line of the print. Written order would have been F1, F2, F3, F4. The print is F1, F3, F4, F2. Those sequences differ at the second line. Quoting the print as "the order Maya typed" would hide the sort.

The id tie happens to agree with F1 appearing before F3 in the source. Agreement is not the mechanism. The mechanism is the second component of the key. Removing that component would make this a different program, even if one run still looked the same.

| Position | Written tuple | Printed line |
|---|---|---|
| First | F1 | F1 |
| Second | F2 | F3 |
| Third | F3 | F4 |
| Fourth | F4 | F2 |

### 12.2 Worked example

| Offered claim | Comparison | Decision |
|---|---|---|
| "F1 before F3 because the id string says so." | `"F1" < "F3"` | Keep |
| "The print matches the source line for line." | Second lines differ | Refuse |
| "F2 finishes last because its title is dull." | Product 2 is the cause | Refuse |

> **Synthetic case.** Four ids, one tie, one reordered tail.

### Errors and limits

Do not invent an id such as F10 and treat it as part of this print.

### Apply

- **EX058 — Tie and tail.** Why does F1 print before F3, and why is F2 last even though it was typed second? *Rubric:* 3 = the shared product defers to the id string, and `"F1"` precedes `"F3"`; F2's product is 2, so its key sorts last; written order is not the print;; 2 = id breaks the tie and the low product finishes last;; 1 = one id;; 0 = none.

<!-- open-cyber-academy:chapter-navigation:start ch-12 -->
### Continue studying — lesson 12 (chapter 12 of 20)

[← Chapter 11](#ch-11) · [Course chapter index](../SUBJECTS.md) · [Chapter 13 →](#ch-13)

Continue to the next authored lesson.
<!-- open-cyber-academy:chapter-navigation:end -->

## Worked answers

<a id="answer-ex058"></a>
**EX058.** F1 and F3 share product 6, and `"F1"` precedes `"F3"`. F2's product is 2, so it sorts last even though it was typed second.
