A sort without thoughtful keys is expensive noise. Keys tell Easytrieve which bytes in each record matter for ordering—and in what priority. USING (REGION, BRANCH, DEPT) means compare REGION first; when two rows share REGION, compare BRANCH; when those tie, compare DEPT. That major-to-minor hierarchy is the same idea behind phone books sorted by last name then first name. Wrong keys or wrong order produce files that look sorted until a control-break report exposes scrambled groups. This page teaches how to pick keys, how DEFINE offsets must match physical layout, how numeric packed and alphanumeric fields compare, which field types are illegal as keys, how SEQUENCE uses the same key rules on spool records, and how to align keys with REPORT CONTROL. Beginners should read this before tuning ascending or descending suffixes or handling duplicates on adjacent keys.
1SORT PERSNL TO SORTWRK USING (REGION, BRANCH, DEPT, EMP#)
REGION is the major key: all region 10 rows form one contiguous group before any region 20 row appears—when ascending. Within region 10, BRANCH orders branches. Within equal REGION and BRANCH, DEPT orders departments. EMP# breaks final ties among employees in the same department. Swapping DEPT before REGION would sort departments globally across regions, destroying regional reporting. Always list keys in the same order you want CONTROL breaks and subtotals.
| Sample rows (REGION, BRANCH, DEPT, EMP#) | Ascending position after sort |
|---|---|
| 10, 01, 100, 5001 | First in group |
| 10, 01, 100, 5002 | After 5001 (EMP# tie-break) |
| 10, 01, 200, 3001 | After DEPT 100 rows |
| 20, 05, 100, 1001 | After all REGION 10 rows |
Sort compares bytes at the offset and length you declared—not at a business meaning you intended. If EMP# is defined 9 5 N but payroll writes employee number at position 1 length 9, sort order follows wrong bytes. After every FILE or copybook change, revalidate key positions against the upstream layout document. Hex dumps of two records that sort wrong often reveal overlapping fields or missing sign nibble on packed decimals.
123456FILE PERSNL FB(150 1800) REGION 1 2 N BRANCH 3 4 N DEPT 7 3 N EMP# 9 5 N NAME 17 20 A
| DEFINE type | Sort comparison | Watch for |
|---|---|---|
| N numeric | Numeric magnitude per signed/unsigned rules | Leading zeros; sign if signed |
| P packed | Numeric order on packed digits | Length includes sign nibble |
| A alphanumeric | EBCDIC collating sequence | ALTSEQ or national characters |
| D date | Date order per date format | Invalid dates sort unpredictably |
| B binary | Binary comparison | Endian and length |
Alternate collating sequences may be installed site-wide or via PARM SORT options when available. Test keys with known low, middle, and high values in a ten-record file before trusting production order on A fields with special characters.
If you need to sort on variable text, copy into a fixed-length W field in BEFORE procedure, SELECT the record, and sort on the W copy—keeping within length limits.
1234REPORT R1 SEQUENCE REGION BRANCH PAY-NET D CONTROL REGION BRANCH LINE REGION BRANCH EMP# PAY-NET
SEQUENCE uses the same major-to-minor and D suffix rules as USING. Keys need not appear on LINE. Spool records contain only fields the report captured at PRINT—ensure PRINT happens after any W fields used as SEQUENCE keys are assigned. SEQUENCE sorts fewer bytes than a full-file SORT when LINE lists only a subset of the input record.
Sort keys define order; they do not deduplicate. Equal keys may appear on many consecutive rows. After sorting, JOB logic can test DUPLICATE, FIRST-DUP, and LAST-DUP when comparing adjacent records on the same key fields—see the Duplicate handling page. Choose keys that reflect business identity (account plus date) before writing duplicate suppression.
Sort keys are the rules for lining up kids for a photo. First rule: tallest to shortest. Second rule: if same height, alphabetical first name. The first rule is major; the second breaks ties. If you use the wrong measurement (shoe size instead of height), the line looks neat but makes no sense—that is a bad key definition.
1. Sort keys in USING appear in:
2. Which field type cannot be a sort key?
3. Each sort key must be:
4. Maximum sort key length is:
5. REPORT CONTROL fields should: