Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 14 - Sorting System Concepts –Sort key Major Key (Primary) Minor Key (Secondary) –Sort sequence Ascending - Low to high Descending – High to.

Similar presentations


Presentation on theme: "1 Chapter 14 - Sorting System Concepts –Sort key Major Key (Primary) Minor Key (Secondary) –Sort sequence Ascending - Low to high Descending – High to."— Presentation transcript:

1 1 Chapter 14 - Sorting System Concepts –Sort key Major Key (Primary) Minor Key (Secondary) –Sort sequence Ascending - Low to high Descending – High to low

2 2 Figure 14.1Sorting Vocabulary NAME YEARMAJOR Smith1Liberal arts Jones4Engineering Adams3Business Howe2Liberal arts Frank1 Engineering Zev4Business Benjamin 4Business Grauer3Liberal arts Crawford 2Engineering Deutsch4Business Makoske 1Business (a) Unsorted Data

3 3 Primary Key: Name (Ascending) NAME YEARMAJOR Adams3Business Benjamin 4Business Crawford 2Engineering Deutsch4Business Frank1 Engineering Grauer3Liberal arts Howe2Liberal arts Jones4Engineering Makoske 1Business Smith1Liberal arts Zev4Business Figure 14.1 (b) Sorted Data, One Key

4 4 Primary key: Year (descending) Secondary key: Name (ascending) NAME YEARMAJOR Benjamin 4Business Deutsch4Business Jones4Engineering Zev4Business Adams3Business Grauer3Liberal arts Crawford 2Engineering Howe2Liberal arts Frank1 Engineering Makoske 1Business Smith1Liberal arts Figure 14.1 Sorted Data, Two Keys

5 5 Figure 14.1 Sorted Data, Three Keys Primary Key: major (Ascending) Secondary Key: year (Descending) Tertiary Key: name (Ascending) NAMEYEARMAJOR Benjamin 4Business Deutsch4Business Zev4Business Adams3Business Makoske 1Business Jones4Engineering Crawford 2Engineering Frank1 Engineering Grauer3Liberal arts Howe2Liberal arts Smith1Liberal arts

6 6 –Collating Sequence (order of sorting) Alphabetic fields –Green vs. Greenfield Alphanumeric fields – 111 vs. AAA EBCDIC sequence – IBM Mainframe ASCII sequence – Most other computers

7 7 EBCDIC (space).(period, decimal point) (greater than) ‘(apostrophe) =(equal sign) “(quotation mark) a through z (lower case) A through Z (upper case) 0 through 9 ASCII (space) “(quotation mark) $(currency symbol) ‘(apostrophe) ((left parenthesis) )(right parenthesis) *(asterisk) +(plus symbol),(comma) -(hyphen, minus symbol).(period, decimal point) /(slash) 0 through 9 ;(semicolon) (greater than) A through Z (upper case) a through z (lower case) Figure 14.2EBCDIC & ASCII Collating Sequences

8 8 COBOL Implementation –SORT command Syntax INPUT PROCEDURE statement –Selective sorting –Sorting on calculated fields USING statement OUTPUT PROCEDURE statement –Use of temporary work file GIVING statement –Retention of sorted data

9 9 SORT file-name-1 ON {DESCENDING KEY { data-name-1}... } ASCENDING} WITH DUPLICATES IN ORDER COLLATING SEQUENCE IS alphabet-name INPUT PROCEDURE IS procedure-name-1 USING {file-name-2} OUTPUT PROCEDURE IS procedure-name-2 GIVING {fine-name-3} SORT syntax

10 10 Uses 3 files to sort –Input file – contains unsorted records –Output file – contains sorted records –Sort file – temporary file used by COBOL to sort the records First 2 must be defined in the FD section Sort file must be defined in a SD section

11 11 Input File Sort Output File Sort file

12 12 Ex.) SD SALESPERSON-SORT-FILE 01 SALESPERSON-SORT-RECORD 05 SP-REGION PIC 99. 05 SP-NUMBER PIC XXXX. 05 FILLER PIC X(52). … SORT SALESPERSON-SORT-FILE ON ASCENDING KEY SP-REGION SP-NUMBER USING SALESPERSON-INPUT-FILE GIVING SALESPERSON-OUTPUT-FILE

13 13 SORT SALESPERSON-SORT-FILE ON DESCENDING KEY SP-REGION ON ASCENDING KEY SP-NUMBER USING SALESPERSON-INPUT-FILE GIVING SALESPERSON-OUTPUT-FILE

14 14 Assume that the records in a file named X-FILE are to be sorted descending sequence by X-FIELD. The records in X-FILE are 40 characters long. X-FIELD is located in positions 8-12 and is alphanumeric. The file sorted output is called Y-FILE a. Code the SD entry and record description b. Code the sort statement c. Assume is should be sorted into ascending sequence by Y-FIELD and then into descending sequence by X- FIELD. Code the sort statements.

15 15 The registrar has asked for a simple report listing students by year, and alphabetically within year. Thus all freshmen are to appear first, then sophomores..The incoming record has the follow layout: 01 STUDENT-RECORD. 05 ST-NAME PIC X(15). 05 ST-MAJOR PIC X(15). 05 ST-YEAR PIC XX. 05 ST-CREDITSPIC X(10). ST-YEAR USES FR,SO,JR AND GR. Develop the procedure division code to accomplish the desired sort.

16 16 Merge statement


Download ppt "1 Chapter 14 - Sorting System Concepts –Sort key Major Key (Primary) Minor Key (Secondary) –Sort sequence Ascending - Low to high Descending – High to."

Similar presentations


Ads by Google