Example: Sort on Calculated Field

 ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\UNSORTC.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\SORTC.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORT-FILE
ASSIGN TO 'C:\PCOBWIN\ROGERS\WORK.TMP'.

DATA DIVISION.
FILE SECTION.
FD IN-FILE
DATA RECORD IS IN-RECORD.
01 IN-RECORD.
02 IN-STUDENT-NAME PIC X(15).
02 IN-CREDITS-TAKEN PIC 9(3).
02 IN-QUALITY-POINTS PIC 9(3).

FD OUT-FILE
DATA RECORD IS OUT-RECORD.
01 OUT-RECORD PIC X(80).

SD SORT-FILE
DATA RECORD IS SORT-RECORD.
01 SORT-RECORD.
02 SORT-STUDENT-NAME PIC X(15).
02 SORT-CREDITS-TAKEN PIC 9(3).
02 SORT-QUALITY-POINTS PIC 9(3).
02 SORT-GPA PIC 9V99.

WORKING-STORAGE SECTION.

01 DETAIL-LINE.
02 DETAIL-STUDENT-NAME PIC X(15).
02 PIC X(3) VALUE SPACES.
02 DETAIL-GPA PIC 9.99.

01 SORTED-DATA-SWITCH PIC X VALUE 'N'.
88 NO-MORE-SORTED-DATA VALUE 'Y'.

01 EOF-SWITCH PIC X VALUE 'N'.
88 NO-MORE-INPUT-RECORDS VALUE 'Y'.

PROCEDURE DIVISION.
MAIN-LOGIC.
SORT SORT-FILE
ON DESCENDING KEY SORT-GPA
INPUT PROCEDURE IS CALCULATE-GPA
OUTPUT PROCEDURE IS PRINT-THE-REPORT.
STOP RUN.

CALCULATE-GPA.
OPEN INPUT IN-FILE.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.
PERFORM DATA-LOOP
UNTIL NO-MORE-INPUT-RECORDS.
CLOSE IN-FILE.

DATA-LOOP.
MOVE IN-STUDENT-NAME TO SORT-STUDENT-NAME.
MOVE IN-CREDITS-TAKEN TO SORT-CREDITS-TAKEN.
MOVE IN-QUALITY-POINTS TO SORT-QUALITY-POINTS.
COMPUTE SORT-GPA ROUNDED =
IN-QUALITY-POINTS / IN-CREDITS-TAKEN.
RELEASE SORT-RECORD.
READ IN-FILE
AT END MOVE 'Y' TO EOF-SWITCH.

PRINT-THE-REPORT.
OPEN OUTPUT OUT-FILE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.
PERFORM REPORT-LOOP
UNTIL NO-MORE-SORTED-DATA.
CLOSE OUT-FILE.

REPORT-LOOP.
MOVE SORT-STUDENT-NAME TO DETAIL-STUDENT-NAME.
MOVE SORT-GPA TO DETAIL-GPA.
WRITE OUT-RECORD FROM DETAIL-LINE.
RETURN SORT-FILE
AT END MOVE 'Y' TO SORTED-DATA-SWITCH.

Input

CHARLIE HACKLEY010040
GORDON HUME 006018
TOMMY BEIDLER 012048
KATHY PECK 040080
MARY UECKER 020070
J.C. SANFORD 006000
DUANE NORTON 015042

Output (report sorted by calculated field G.P.A.)

CHARLIE HACKLEY   4.00
TOMMY BEIDLER 4.00
MARY UECKER 3.50
GORDON HUME 3.00
DUANE NORTON 2.80
KATHY PECK 2.00
J.C. SANFORD 0.00