Sorting by Functions in COBOL

Our program demonstrates sorting collections using custom comparison functions. Here’s the full source code:

IDENTIFICATION DIVISION.
PROGRAM-ID. SORTING-BY-FUNCTIONS.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT STANDARD-OUTPUT ASSIGN TO DISPLAY
    ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD STANDARD-OUTPUT.
01 OUTPUT-LINE PIC X(80).

WORKING-STORAGE SECTION.
01 FRUITS.
    05 FRUIT-ITEM OCCURS 3 TIMES.
        10 FRUIT-NAME PIC X(10).
01 FRUIT-INDEX PIC 9(2).
01 I PIC 9(2).
01 J PIC 9(2).
01 TEMP-FRUIT PIC X(10).

01 PEOPLE.
    05 PERSON OCCURS 3 TIMES.
        10 PERSON-NAME PIC X(10).
        10 PERSON-AGE PIC 9(3).
01 PEOPLE-INDEX PIC 9(2).
01 TEMP-PERSON.
    05 TEMP-NAME PIC X(10).
    05 TEMP-AGE PIC 9(3).

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM INITIALIZE-DATA
    PERFORM SORT-FRUITS
    PERFORM DISPLAY-FRUITS
    PERFORM SORT-PEOPLE
    PERFORM DISPLAY-PEOPLE
    STOP RUN.

INITIALIZE-DATA.
    MOVE "PEACH" TO FRUIT-NAME(1)
    MOVE "BANANA" TO FRUIT-NAME(2)
    MOVE "KIWI" TO FRUIT-NAME(3)
    
    MOVE "JAX" TO PERSON-NAME(1)
    MOVE 37 TO PERSON-AGE(1)
    MOVE "TJ" TO PERSON-NAME(2)
    MOVE 25 TO PERSON-AGE(2)
    MOVE "ALEX" TO PERSON-NAME(3)
    MOVE 72 TO PERSON-AGE(3).

SORT-FRUITS.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
        PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3 - I
            IF FUNCTION LENGTH(FRUIT-NAME(J)) > 
               FUNCTION LENGTH(FRUIT-NAME(J + 1))
                MOVE FRUIT-NAME(J) TO TEMP-FRUIT
                MOVE FRUIT-NAME(J + 1) TO FRUIT-NAME(J)
                MOVE TEMP-FRUIT TO FRUIT-NAME(J + 1)
            END-IF
        END-PERFORM
    END-PERFORM.

DISPLAY-FRUITS.
    MOVE "[" TO OUTPUT-LINE
    PERFORM VARYING FRUIT-INDEX FROM 1 BY 1 UNTIL FRUIT-INDEX > 3
        STRING OUTPUT-LINE DELIMITED BY SPACE
               FRUIT-NAME(FRUIT-INDEX) DELIMITED BY SPACE
               ", " DELIMITED BY SIZE
               INTO OUTPUT-LINE
    END-PERFORM
    STRING OUTPUT-LINE DELIMITED BY ", "
           "]" DELIMITED BY SIZE
           INTO OUTPUT-LINE
    WRITE OUTPUT-LINE.

SORT-PEOPLE.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
        PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3 - I
            IF PERSON-AGE(J) > PERSON-AGE(J + 1)
                MOVE PERSON(J) TO TEMP-PERSON
                MOVE PERSON(J + 1) TO PERSON(J)
                MOVE TEMP-PERSON TO PERSON(J + 1)
            END-IF
        END-PERFORM
    END-PERFORM.

DISPLAY-PEOPLE.
    MOVE "[" TO OUTPUT-LINE
    PERFORM VARYING PEOPLE-INDEX FROM 1 BY 1 UNTIL PEOPLE-INDEX > 3
        STRING OUTPUT-LINE DELIMITED BY SPACE
               "{" DELIMITED BY SIZE
               PERSON-NAME(PEOPLE-INDEX) DELIMITED BY SPACE
               " " DELIMITED BY SIZE
               PERSON-AGE(PEOPLE-INDEX) DELIMITED BY SIZE
               "}, " DELIMITED BY SIZE
               INTO OUTPUT-LINE
    END-PERFORM
    STRING OUTPUT-LINE DELIMITED BY ", "
           "]" DELIMITED BY SIZE
           INTO OUTPUT-LINE
    WRITE OUTPUT-LINE.

This COBOL program demonstrates custom sorting of collections. We sort a list of fruits by their name length and a list of people by their age.

First, we initialize our data structures with sample data. Then, we implement custom sorting functions for both fruits and people.

For fruits, we use a bubble sort algorithm comparing the length of fruit names:

SORT-FRUITS.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
        PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3 - I
            IF FUNCTION LENGTH(FRUIT-NAME(J)) > 
               FUNCTION LENGTH(FRUIT-NAME(J + 1))
                MOVE FRUIT-NAME(J) TO TEMP-FRUIT
                MOVE FRUIT-NAME(J + 1) TO FRUIT-NAME(J)
                MOVE TEMP-FRUIT TO FRUIT-NAME(J + 1)
            END-IF
        END-PERFORM
    END-PERFORM.

For people, we sort based on their age:

SORT-PEOPLE.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
        PERFORM VARYING J FROM 1 BY 1 UNTIL J > 3 - I
            IF PERSON-AGE(J) > PERSON-AGE(J + 1)
                MOVE PERSON(J) TO TEMP-PERSON
                MOVE PERSON(J + 1) TO PERSON(J)
                MOVE TEMP-PERSON TO PERSON(J + 1)
            END-IF
        END-PERFORM
    END-PERFORM.

After sorting, we display the results. The output will show the fruits sorted by name length and people sorted by age.

Note: COBOL doesn’t have built-in sorting functions like some modern languages, so we implement the sorting algorithms manually. In a real-world scenario, you might want to use more efficient sorting algorithms for larger datasets.

To run this COBOL program, you would typically compile it and then execute the resulting binary. The exact commands may vary depending on your COBOL compiler and environment.