Easytrieve Multi-Key Sorting

Real files rarely sort on one column. Payroll lists need region, then branch, then department, then employee. General ledger extracts need company, account, subaccount, and posting sequence. Multi-key sorting is the art of listing those fields in USING or SEQUENCE so major groups stay intact while minor keys break ties. Swap one position and the file still sorts—but control breaks lie, subtotals double-count, and operators lose trust. Mixed directions add power: ascending hierarchy with descending amounts within the lowest group. This page walks multi-key design end to end: hierarchy diagrams, payroll and GL examples, CONTROL nesting, SEQUENCE alignment, per-key D combinations, installation key limits, performance habits, duplicate groups within minor keys, and a checklist before you commit keys to production. Single-key ascending and descending tutorials cover basics; this is the integration chapter for production layouts.

Progress0 of 0 lessons

Major-to-Minor Stack

text
1
2
3
4
5
6
7
8
SORT PERSNL TO SORTWRK USING (REGION, BRANCH, DEPT, EMP#) REPORT PAY-RPT LINESIZE 132 SEQUENCE REGION BRANCH DEPT EMP# CONTROL REGION BRANCH DEPT TITLE 01 'PAYROLL REGISTER' LINE 01 REGION BRANCH DEPT EMP# NAME GROSS SUM GROSS

Four keys define four nested grouping levels before the unique employee tie-breaker. CONTROL lists three break levels—REGION, BRANCH, DEPT—so subtotals fire at department change, branch change, and region change. EMP# is minor key for stable row order within department without an extra control level unless you add CONTROL EMP# for employee-only lists.

Hierarchy Diagram

How four keys nest (ascending)
LevelKeyWhen value changes
MajorREGIONNew region block begins
MinorBRANCHNew branch within same region
More minorDEPTNew department within branch
Tie-breakEMP#Order among same dept employees

Mixed D on Multiple Keys

text
1
SORT PERSNL TO SORTWRK USING (REGION, BRANCH, DEPT, GROSS D, EMP#)

REGION, BRANCH, DEPT, EMP# ascend. GROSS D ranks highest pay first among employees tied on department and other equal keys—actually within equal REGION BRANCH DEPT, GROSS orders descending before EMP# tie-breaks ascending. Document D placement in run books; future editors may think GROSS D sorts entire company by pay if they ignore hierarchy.

Three-Level CONTROL Subtotals

Each CONTROL field generates break processing when its value changes. With REGION BRANCH DEPT, department totals print first (most minor control), then branch totals when branch changes, then region totals. FINAL still prints grand totals. SUM GROSS must include fields you want accumulated at each level. LINE order should place amount columns where readers expect subtotal alignment.

When to Add or Remove Keys

  • Add a key when a new subtotal level is required.
  • Add EMP# or sequence as final tie-breaker for stable duplicate handling.
  • Remove keys you do not break on to reduce sort cost.
  • Do not add keys not defined on FILE layout.
  • Hide keys from LINE but keep on SEQUENCE when only order needs them.

GL-Style Five-Key Example

text
1
2
3
4
SORT GLDET TO SORTWRK USING (CO, ACCT, SUBACCT, POST-DATE, SEQ-NO) JOB INPUT SORTWRK PRINT GL-RPT

Company major, account, subaccount, posting date, sequence minor—classic accounting trail. Posting date ascending with SEQ-NO minor preserves intra-day order. For newest-first within account, apply D only to POST-DATE and verify LAST-DUP dedup patterns if needed.

SEQUENCE Subset Versus Full USING

File sorted on four keys; report SEQUENCE on two may reorder spool relative to file for that report only—usually avoided when one report consumes the sorted file directly. Typical pattern: SORT full keys once, JOB reads sorted file, REPORT CONTROL matches SORT without conflicting SEQUENCE. SEQUENCE alone on a flat unsorted JOB INPUT is valid when only print order matters and file order can stay random.

Installation Key Limits

Broadcom notes maximum keys depends on the sort program installation. Extremely wide key lists hit limits or degrade performance. If you need many key bytes, consider pre-sort in DFSORT with INCLUDE and OUTREC keeping only key fields plus pointer—advanced pattern outside beginner EASYTRIEVE SORT.

Multi-Key and Duplicates

Duplicate grain equals the key set you test in JOB. If duplicates are defined as same ACCT and POST-DATE, both must appear in USING before FIRST-DUP on those fields means duplicate group. Sorting only ACCT groups all dates together—duplicate date logic breaks.

Design Checklist

  1. List subtotal levels the business requires.
  2. Map each level to a CONTROL field.
  3. Order USING keys major-to-minor matching CONTROL.
  4. Add tie-breaker key (EMP#, SEQ).
  5. Apply D only where business reverses order.
  6. Align SEQUENCE or omit when file already sorted.
  7. Test with two values per level minimum.

Common Multi-Key Mistakes

  • CONTROL order unlike USING order.
  • Too few keys for required subtotals.
  • Major-key D breaking nested groups.
  • SEQUENCE fighting SORT order on same report.
  • Omitting tie-breaker—unstable duplicate order.

Explain It Like I'm Five

Multi-key sorting is sorting a big box of toys first by color, then by size inside each color, then by type inside each size. CONTROL is saying when to put a divider card between color piles, size piles, and type piles. If you sort by size before color, the color piles get mixed up even though the box still looks organized.

Exercises

  1. Design four-key USING for a fictional payroll file.
  2. Write matching CONTROL and SUM for GROSS.
  3. Add GROSS D as minor key; describe printed order.
  4. Identify one report that needs only SEQUENCE two keys.
  5. Run checklist on an existing production SORT in your shop.

Quiz

Test Your Knowledge

1. In USING (A, B, C), key C is:

  • Most minor—tie-breaks within equal A and B
  • Major over A and B
  • Ignored if D absent
  • Only for TITLE

2. CONTROL REGION BRANCH DEPT should align with:

  • SORT USING on the same keys in the same order
  • Random LINE fields
  • JCL only
  • SCREEN keys

3. USING (REGION, BRANCH D, DEPT, EMP#) has how many keys?

  • Four
  • One
  • Two
  • D counts as a key

4. Too many sort keys can:

  • Hit installation sort limits and cost CPU
  • Always speed up sort
  • Disable PRINT
  • Remove FILE

5. Nested subtotals need:

  • CONTROL listing each break level matching sort hierarchy
  • TITLE only
  • No SEQUENCE
  • GET PRIOR
Published
Read time18 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 multi-key USING and CONTROL alignmentSources: Broadcom Easytrieve 11.6 Language Reference SORT, REPORT CONTROL SEQUENCEApplies to: Easytrieve multi-key sort and control-break design