- The COBOL language includes a powerful sort statement that can sort large amounts of data. The data can be sorted by a single field, or multiple fields combined to make a more complex key structure. Let's take a look at an example. It is often helpful to sort the data coming into a program. On the right, here, you see a file containing a list of students. But they're not in any sorted order. The information is the student ID number, student's last name, first name, middle initial, date of birth, major, and finally, gender.
I have the sorting program all ready. Let's compile it, run it and see what happens. So to compile, I do cobc dash x and the name of the program. Next, I can run it. OK, now that I compiled and ran it, lets take a look at the output. As you can see here, on line 11, it creates a new file called STUDENTS.NEW. OK, well, it looks like it sorted the students by major. You can see line one is Danielle and she's a bio major.
There's three business majors, a chemistry major, a few comp sci, some IST, some math, all right. So it looks like the sort worked. Now, we want to add sort criteria to sort the students by name within major. So we will use the major as the primary key, the last name and first name as the secondary sort keys. Let's try it again and take a look at the results. So I'm going to scroll down to the sort, and I'm going to add two more key fields. I'm going to add WSTUDENTLNAME and WSTUDENTFNAME.
I'm going to save my file, I'm going to compile it again, and run it. Now, let's go back over and open up the new file. It's the very last one, it says NEW File here, so STUDENTS. And if I open it up, it opened on the right this time, you'll see that, for example, within the business majors, it's now sorting by last name: Hubert, Kingston, Morgan. Let's take a look at the engineering majors.
They're on lines 11 through 16, and you can see now they're also sorted alphabetically by last name within major. Cool, it looks like it worked. Let's take a closer look at the code. In order to use the sort statement, we need to first add a temporary file in our file-control section. So, let me scroll back up. On line 14, you'll notice there's a third file that says SELECT WORKFILE ASSIGN TO WORK.TMP, for temporary. This file won't exist when my program's done, but is a temporary working storage space to sort the file.
Next, we need to define the sort description for the file being sorted. As you can see, this starts on line 24. I have SD instead of FD, that's for sort description and the name of it is WORKFILE. Notice, the only variable names provided are the fields that I am using for sorting, the WSTUDENTLNAME, WSTUDENTFNAME and WMAJOR. Finally, let's scroll down to the main paragraph, READ-EMPLOYEES. Here we open the input file STUDENTFILE, and then we have our sort statement.
SORT WORKFILE ON ASCENDING KEY WMAJOR followed by WSTUDENTLNAME and WSTUDENTFNAME USING STUDENTFILE GIVING SORTEDFILE. Then we go to 9000-END-PROGRAM, where we close the file and STOP RUN. There's not a whole lot of code; a lot of it's done by the COBOL program itself. The data may originate from within the program, or may be contained in one or more external files, which is what we just saw.
The sorted data may be written automatically to one or more output files, or it may be processed in the program, record by record, in the sorted sequence. For example, we might want to create a report of all females in each major. Or a report that counts the number of students in each major. Now that the data is sorted, the report is a lot easier. Finally, I want to mention that a special form of the sort statement also exists just to sort the data that resides in a table. This is particularly useful if you wish to use SEARCH ALL against the table.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings