TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Sort Example 1

In the following example, INPUT is the input file which needs to be sorted in ascending order

IDENTIFICATION DIVISION.
PROGRAM-ID. CBLSORT1.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
      SELECT INPUT ASSIGN TO IN.
      SELECT OUTPUT ASSIGN TO OUT.
      SELECT WORK ASSIGN TO WRK.

DATA DIVISION.
FILE SECTION.
FD INPUT.
01 INPUT-STUDENT.
   05 STUDENT-ID-I PIC 9(5).
   05 STUDENT-NAME-I PIC A(25).

FD OUTPUT.
01 OUTPUT-STUDENT.
   05 STUDENT-ID-O PIC 9(5).
   05 STUDENT-NAME-O PIC A(25).

SD WORK.
01 WORK-STUDENT.
   05 STUDENT-ID-W PIC 9(5).
   05 STUDENT-NAME-W PIC A(25).

PROCEDURE DIVISION.
   SORT WORK ON ASCENDING KEY STUDENT-ID-O
   USING INPUT GIVING OUTPUT.
   DISPLAY 'Sort Successful'.
   STOP RUN.

When you compile and execute the above program, it produces the following result −

Sort Successful


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal