Hello, this is Kevin welcoming you to another lesson. This one is on sorting and merging. How and where do you cook a meal? Many of us use indoor stoves but sometimes it's fun to cook outdoors. [SOUND] I can smell an outdoor barbecue even now. Well, sorting and merging can be handled by coding with job control language and a utility program which is easily visible by everyone kind of like being at an outdoor meal. This is called standalone sort. Other times we use COBOL but we need to look indoors to see what is going on. This is called an internal sort. Sorting with COBOL is our main topic in this lesson. During this lesson you will discover how to describe the sort / merge processes, add the required code to the environment, data, and procedure divisions for sort and merging. Describe the input phase, the sort phase, and the output phase of sorting within the COBOL program. Much of laundry needs to be sorted so does data. How does data get sorted? In a previous lesson we revealed two systems for data, EBCDIC used by IBM and some other vendors and ASCII used by everyone else. The sorting sequence for EBCDIC is different than the sorting sequence for ASCII. For example, lower case letters get sorted ahead of uppercase letters with EBCDIC, but upper case letters get sorted before lower case letters with ASCII. EBCDIC gets letters before numbers, while ASCII does the exact opposite. Let's see what the sort/merge process looks like. Changing the sequence of records is often needed and is accomplished with the sort verb or the merge verb. Sort is going to reorder records into a desired sequence while merge combines two or more files that have already been put in sequence and now were being put into one single file. Sort and merge follow a three step phase, input, sort, and output. The input phase receives records and passes them to the sort phase. The sort phase organizes the records based on sort keys using a file defined by an SD entry. SD is a sort description, and that's placed in the file section. The output phase returns records to either an output file or back to the program for some additional processing. You heard me talk about key parameters. What is a key parameter? In this case, a parameter lets us choose how sorting will operate. We can sort in ascending sequence, going from lowest to highest or descending, going from highest to lowest. We can sort with fixed or with variable length records. Key fields tell the program how to sort the data, like by letters or by numbers. You can have multiple keys. As a matter of fact, there is no limit. As long as they do not take up more than 4092 bytes. To sort with COBOL, we need entries to the environment, data, and procedure divisions. In the environment division we add the select sort file statement within the file control entry. Next we add to the data division in SD sort description which is like a file description. It's coded in the A margin providing the record size of 80, the name of the file which is sort employee, and the fields first-name and last-name. Be aware that the sort description cannot do everything that the file description does. The data division is repeated on top of the screen, along with the procedure division where we see two keys. There's a key for last name and a key for first name that will sort the records of the input file, then store them in the output file. There are many options with sorting. We're only going to look at a few. An input procedure can take the place of a file like input-file on your screen. An output procedure can take the place of a file like output-file on your screen. You can also mix and match these. On the previous slide we saw using and giving options. What do they do? They do a lot. Look at the top where we have the word using. With using records are immediately processed by the SORT utility. This is a sort that doesn't have any additional logic. The compiler automatically generates an open file, read the records, release to the sort, and close the file. The second item towards the bottom of your screen is on giving. With these, sorted records are immediately written to the output files. Records are not processed before writing, the compiler automatically generates the output procedure to open the file, read the records, write the records, and close the file. Starting on this page, we're going to have three pages where we will describe the input, release, and output procedures. So, first, right here we begin with input procedure. The input procedure is in the procedure division as you might guess. It opens the input file, reads and processes records before sorting, releases records one at a time to the sort, closes the input file. And there's some restrictions, we cannot use sort, merge, goback, stop run, exit, exit program verbs. It does use release to sort. Second, we have an input procedure that uses release. An input procedure is written to choose records that will be sorted and those records that will not be sorted. For a record that is being sorted we don't code the word WRITE, we code the word RELEASE. See release, transfers chosen records to a sorting operation. Release is coded in the input procedure. An example can be seen at the bottom of your screen. The third is taking a look at the output procedure. It is coded in the procedure division. It processes, records one at a time after sorting. And we cannot code sort, merge, goback, stop run, exit, and exit program verbs. So during this lesson, we've discovered how to describe the sort / merge process. Added the required code to the environment, data, and procedure divisions in order to sort the data. And described the input phase, sort phase, and output phase of sorting. See you soon.