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.
12345678SORT 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.
| Level | Key | When value changes |
|---|---|---|
| Major | REGION | New region block begins |
| Minor | BRANCH | New branch within same region |
| More minor | DEPT | New department within branch |
| Tie-break | EMP# | Order among same dept employees |
1SORT 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.
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.
1234SORT 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.
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.
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.
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.
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.
1. In USING (A, B, C), key C is:
2. CONTROL REGION BRANCH DEPT should align with:
3. USING (REGION, BRANCH D, DEPT, EMP#) has how many keys?
4. Too many sort keys can:
5. Nested subtotals need: