SECTION 08 / 28

Sort by one component and keep the whole pair

1 min read
For [ID,Physics] pairs, compare the last component. [1,45] comes before [3,53] in ascending Physics order.
For [ID,Physics] pairs, compare the last component. [1,45] comes before [3,53] in ascending Physics order.
REAL COURSE DATA · SORT, GROUP AND VERIFY

Build a sorted view of score pairs

Follow the selected procedure. Change a field, tie rule or weekday and explain why the result changes.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change. Keep track of which list level, field and position convention the procedure uses.

# In SortedListInsert for [ID,Mark] pairs:
if last(x) < last(z):
    newList = newList ++ [x]
    inserted = true
# Always append the WHOLE z pair after the test.

# Build pairs for a chosen subject:
marksList = []
foreach X in ScoreRecords:
    marksList = marksList ++ [[X.CardNo,X[Subject]]]

The outer list contains pairs; first(pair) is the ID and last(pair) is the selected mark. Comparing entire pair objects may use the wrong ordering in a programming language. State that the mark is the key. Only the comparison changes; the outer insertion pattern remains the same.

The source’s BuildMarksList moves rows from one table to another. Restore the data between consuming calls for Maths and Physics. Our explorer reads the existing records without consuming them, so several views can be produced safely.

Try it yourself

Why not sort [ID,mark] pairs by first(pair)?

Show explained answer

That sorts IDs rather than marks.

Try it yourself

When moving a pair, do you copy only its key?

Show explained answer

No. Copy both ID and mark so their association survives.

Try it yourself

What does last(first(marksList)) return on a non-empty pair list?

Show explained answer

The mark stored in the first pair, not the entire pair or the student ID.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑