Skip to main content
Module 7 · Lesson 1 of 213/16 in the course~16 min
Module lessons (1/2)

Batch sorting with SORT and MERGE

Lesson preview5 hands-on exercises readyCourse COBOL · Module 7 — File Sorting, Copybook and Level 88

Define temporary files SD (Sort Description), sort by ASCENDING/DESCENDING keys with SORT USING/GIVING, and merge sorted streams via MERGE.

What you'll learn in this lesson

  • Declare a temporary working file with SD (Sort Description) in FILE SECTION.
  • Sort sequential archives on one or more keys with SORT ... ON ASCENDING/DESCENDING KEY ... USING ... GIVING ....
  • Merge multiple already sorted files into a single output with MERGE ... USING ... GIVING ....
  • Understand when to replace USING/GIVING with INPUT PROCEDURE and OUTPUT PROCEDURE for streaming filtering or aggregation.

Overview

In financial batches, sorting millions of transactions by account and date before reconciliation is a daily operation. COBOL integrates the SORT and MERGE verbs directly into the syntax of the language.

A work file used for sorting does not have a permanent path: in ENVIRONMENT DIVISION you associate it with a work file and in FILE SECTION you define it with SD instead of FD:

Sections covered in this lesson

  1. 11. The temporary descriptor SD (Sort Description)
  2. 22. Multi-key sorting with SORT ... USING ... GIVING
  3. 33. Quick Merge with MERGE
  4. 44. Streaming filtering with INPUT / OUTPUT PROCEDURE
  5. 5Try it yourself
Pro Lesson · Module 7 — File Sorting, Copybook and Level 88

Unlock "Batch sorting with SORT and MERGE" and the full Course COBOL curriculum

Define temporary files SD (Sort Description), sort by ASCENDING/DESCENDING keys with SORT USING/GIVING, and merge sorted streams via MERGE.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course COBOL plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.