MainframeMaster

COBOL Data Ordering Concepts

Data ordering in COBOL involves arranging data in specific sequences or orders to meet business requirements, improve processing efficiency, and facilitate data retrieval. This includes sorting operations, collating sequences, data organization techniques, and sequencing methods. Understanding data ordering concepts is essential for building efficient COBOL applications that process large volumes of data in meaningful sequences.

Understanding Data Ordering

Data ordering in COBOL encompasses all methods of arranging data in specific sequences, including sorting by key fields, organizing data by business rules, and maintaining data in logical order. Proper data ordering improves processing efficiency, enables faster data retrieval, and supports business logic requirements. Different ordering methods are appropriate for different data types and processing scenarios.

Sorting Fundamentals

1. Basic Sorting Operations

Basic sorting operations in COBOL involve arranging data records in ascending or descending order based on one or more key fields. The SORT statement provides powerful sorting capabilities with support for multiple sort keys, collating sequences, and various sort options. Understanding basic sorting is fundamental to data ordering concepts.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
IDENTIFICATION DIVISION. PROGRAM-ID. DATA-ORDERING-BASICS. *> This program demonstrates basic data ordering concepts DATA DIVISION. WORKING-STORAGE SECTION. *> Data structures for sorting demonstration 01 SORTING-DATA. 05 SORT-ARRAY OCCURS 10 TIMES. 10 CUSTOMER-ID PIC 9(5). 10 CUSTOMER-NAME PIC X(20). 10 CUSTOMER-BALANCE PIC 9(8)V99. 10 CUSTOMER-CITY PIC X(15). 01 SORTING-CONTROLS. 05 SORT-MODE PIC X(1). 88 SORT-ASCENDING VALUE 'A'. 88 SORT-DESCENDING VALUE 'D'. 05 SORT-KEY-TYPE PIC X(1). 88 SORT-BY-ID VALUE 'I'. 88 SORT-BY-NAME VALUE 'N'. 88 SORT-BY-BALANCE VALUE 'B'. 88 SORT-BY-CITY VALUE 'C'. 01 SORTING-RESULTS. 05 SORT-COUNT PIC 9(2). 05 SORT-STATUS PIC X(1). 88 SORT-SUCCESS VALUE 'S'. 88 SORT-FAILURE VALUE 'F'. PROCEDURE DIVISION. DATA-ORDERING-MAIN. DISPLAY 'Data Ordering Concepts Demonstration' DISPLAY '===================================' *> Initialize test data PERFORM INITIALIZE-TEST-DATA *> Demonstrate basic sorting PERFORM DEMONSTRATE-BASIC-SORTING *> Demonstrate multi-key sorting PERFORM DEMONSTRATE-MULTI-KEY-SORTING *> Demonstrate custom ordering PERFORM DEMONSTRATE-CUSTOM-ORDERING STOP RUN. INITIALIZE-TEST-DATA. *> Initialize test data for sorting demonstration DISPLAY 'Initializing Test Data:' MOVE 10001 TO CUSTOMER-ID(1) MOVE 'Smith, John' TO CUSTOMER-NAME(1) MOVE 1500.50 TO CUSTOMER-BALANCE(1) MOVE 'New York' TO CUSTOMER-CITY(1) MOVE 10005 TO CUSTOMER-ID(2) MOVE 'Brown, Mary' TO CUSTOMER-NAME(2) MOVE 2500.75 TO CUSTOMER-BALANCE(2) MOVE 'Chicago' TO CUSTOMER-CITY(2) MOVE 10002 TO CUSTOMER-ID(3) MOVE 'Davis, Bob' TO CUSTOMER-NAME(3) MOVE 800.25 TO CUSTOMER-BALANCE(3) MOVE 'Los Angeles' TO CUSTOMER-CITY(3) MOVE 10004 TO CUSTOMER-ID(4) MOVE 'Wilson, Sue' TO CUSTOMER-NAME(4) MOVE 3200.00 TO CUSTOMER-BALANCE(4) MOVE 'Houston' TO CUSTOMER-CITY(4) MOVE 10003 TO CUSTOMER-ID(5) MOVE 'Johnson, Tom' TO CUSTOMER-NAME(5) MOVE 1200.00 TO CUSTOMER-BALANCE(5) MOVE 'Phoenix' TO CUSTOMER-CITY(5) MOVE 5 TO SORT-COUNT DISPLAY 'Test data initialized with ' SORT-COUNT ' records' DISPLAY ' '. DEMONSTRATE-BASIC-SORTING. *> Demonstrate basic sorting operations DISPLAY 'Basic Sorting Demonstration:' DISPLAY 'Original order:' PERFORM DISPLAY-CUSTOMER-DATA *> Sort by customer ID (ascending) SET SORT-BY-ID TO TRUE SET SORT-ASCENDING TO TRUE PERFORM SORT-BY-SINGLE-KEY DISPLAY 'Sorted by Customer ID (ascending):' PERFORM DISPLAY-CUSTOMER-DATA *> Sort by customer name (ascending) SET SORT-BY-NAME TO TRUE PERFORM SORT-BY-SINGLE-KEY DISPLAY 'Sorted by Customer Name (ascending):' PERFORM DISPLAY-CUSTOMER-DATA *> Sort by balance (descending) SET SORT-BY-BALANCE TO TRUE SET SORT-DESCENDING TO TRUE PERFORM SORT-BY-SINGLE-KEY DISPLAY 'Sorted by Balance (descending):' PERFORM DISPLAY-CUSTOMER-DATA DISPLAY ' '. SORT-BY-SINGLE-KEY. *> Sort array by single key IF SORT-BY-ID PERFORM SORT-BY-CUSTOMER-ID ELSE IF SORT-BY-NAME PERFORM SORT-BY-CUSTOMER-NAME ELSE IF SORT-BY-BALANCE PERFORM SORT-BY-CUSTOMER-BALANCE ELSE IF SORT-BY-CITY PERFORM SORT-BY-CUSTOMER-CITY END-IF END-IF END-IF END-IF. SORT-BY-CUSTOMER-ID. *> Sort by customer ID PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF SORT-ASCENDING IF CUSTOMER-ID(SORT-I) > CUSTOMER-ID(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF ELSE IF CUSTOMER-ID(SORT-I) < CUSTOMER-ID(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-IF END-PERFORM END-PERFORM. SORT-BY-CUSTOMER-NAME. *> Sort by customer name PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF SORT-ASCENDING IF CUSTOMER-NAME(SORT-I) > CUSTOMER-NAME(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF ELSE IF CUSTOMER-NAME(SORT-I) < CUSTOMER-NAME(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-IF END-PERFORM END-PERFORM. SORT-BY-CUSTOMER-BALANCE. *> Sort by customer balance PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF SORT-ASCENDING IF CUSTOMER-BALANCE(SORT-I) > CUSTOMER-BALANCE(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF ELSE IF CUSTOMER-BALANCE(SORT-I) < CUSTOMER-BALANCE(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-IF END-PERFORM END-PERFORM. SORT-BY-CUSTOMER-CITY. *> Sort by customer city PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF SORT-ASCENDING IF CUSTOMER-CITY(SORT-I) > CUSTOMER-CITY(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF ELSE IF CUSTOMER-CITY(SORT-I) < CUSTOMER-CITY(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-IF END-PERFORM END-PERFORM. SWAP-CUSTOMER-RECORDS. *> Swap two customer records MOVE CUSTOMER-ID(SORT-I) TO TEMP-CUSTOMER-ID MOVE CUSTOMER-NAME(SORT-I) TO TEMP-CUSTOMER-NAME MOVE CUSTOMER-BALANCE(SORT-I) TO TEMP-CUSTOMER-BALANCE MOVE CUSTOMER-CITY(SORT-I) TO TEMP-CUSTOMER-CITY MOVE CUSTOMER-ID(SORT-J) TO CUSTOMER-ID(SORT-I) MOVE CUSTOMER-NAME(SORT-J) TO CUSTOMER-NAME(SORT-I) MOVE CUSTOMER-BALANCE(SORT-J) TO CUSTOMER-BALANCE(SORT-I) MOVE CUSTOMER-CITY(SORT-J) TO CUSTOMER-CITY(SORT-I) MOVE TEMP-CUSTOMER-ID TO CUSTOMER-ID(SORT-J) MOVE TEMP-CUSTOMER-NAME TO CUSTOMER-NAME(SORT-J) MOVE TEMP-CUSTOMER-BALANCE TO CUSTOMER-BALANCE(SORT-J) MOVE TEMP-CUSTOMER-CITY TO CUSTOMER-CITY(SORT-J). DEMONSTRATE-MULTI-KEY-SORTING. *> Demonstrate multi-key sorting DISPLAY 'Multi-Key Sorting Demonstration:' *> Sort by city first, then by name within city PERFORM SORT-BY-CITY-THEN-NAME DISPLAY 'Sorted by City, then by Name:' PERFORM DISPLAY-CUSTOMER-DATA DISPLAY ' '. SORT-BY-CITY-THEN-NAME. *> Sort by city first, then by name within city PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF CUSTOMER-CITY(SORT-I) > CUSTOMER-CITY(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS ELSE IF CUSTOMER-CITY(SORT-I) = CUSTOMER-CITY(SORT-J) IF CUSTOMER-NAME(SORT-I) > CUSTOMER-NAME(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-IF END-IF END-PERFORM END-PERFORM. DEMONSTRATE-CUSTOM-ORDERING. *> Demonstrate custom ordering logic DISPLAY 'Custom Ordering Demonstration:' *> Order by business rules (high balance customers first) PERFORM ORDER-BY-BUSINESS-RULES DISPLAY 'Ordered by Business Rules (High Balance First):' PERFORM DISPLAY-CUSTOMER-DATA DISPLAY ' '. ORDER-BY-BUSINESS-RULES. *> Order by business rules PERFORM VARYING SORT-I FROM 1 BY 1 UNTIL SORT-I > SORT-COUNT - 1 PERFORM VARYING SORT-J FROM SORT-I + 1 BY 1 UNTIL SORT-J > SORT-COUNT IF CUSTOMER-BALANCE(SORT-I) < CUSTOMER-BALANCE(SORT-J) PERFORM SWAP-CUSTOMER-RECORDS END-IF END-PERFORM END-PERFORM. DISPLAY-CUSTOMER-DATA. *> Display customer data PERFORM VARYING DISPLAY-INDEX FROM 1 BY 1 UNTIL DISPLAY-INDEX > SORT-COUNT DISPLAY ' ' CUSTOMER-ID(DISPLAY-INDEX) ' ' CUSTOMER-NAME(DISPLAY-INDEX) ' ' CUSTOMER-BALANCE(DISPLAY-INDEX) ' ' CUSTOMER-CITY(DISPLAY-INDEX) END-PERFORM.

This example demonstrates basic data ordering concepts including single-key sorting, multi-key sorting, and custom ordering logic. The program shows how to sort data by different fields (ID, name, balance, city), implement multi-key sorting where secondary keys are used to break ties, and apply custom business rules for data ordering. The sorting algorithms use bubble sort methodology for simplicity and clarity.

2. Advanced Sorting Techniques

Advanced sorting techniques include using the SORT statement with external sort utilities, implementing efficient sorting algorithms, handling large datasets, and optimizing sort performance. These techniques provide more sophisticated ways to order data and handle complex sorting requirements.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
IDENTIFICATION DIVISION. PROGRAM-ID. ADVANCED-SORTING-TECHNIQUES. *> This program demonstrates advanced sorting techniques DATA DIVISION. WORKING-STORAGE SECTION. *> Advanced sorting data structures 01 SORTING-ALGORITHMS. 05 ALGORITHM-TYPE PIC X(1). 88 BUBBLE-SORT VALUE 'B'. 88 SELECTION-SORT VALUE 'S'. 88 INSERTION-SORT VALUE 'I'. 88 QUICK-SORT VALUE 'Q'. 05 SORT-PERFORMANCE. 10 COMPARISON-COUNT PIC 9(6). 10 SWAP-COUNT PIC 9(6). 10 SORT-TIME PIC 9(8). 01 LARGE-DATASET. 05 LARGE-ARRAY OCCURS 100 TIMES. 10 RECORD-ID PIC 9(6). 10 RECORD-DATA PIC X(50). 10 RECORD-VALUE PIC 9(8)V99. 01 SORTING-OPTIONS. 05 SORT-STABILITY PIC X(1). 88 STABLE-SORT VALUE 'Y'. 88 UNSTABLE-SORT VALUE 'N'. 05 SORT-MEMORY PIC X(1). 88 IN-MEMORY-SORT VALUE 'Y'. 88 EXTERNAL-SORT VALUE 'N'. 01 PERFORMANCE-METRICS. 05 START-TIME PIC 9(8). 05 END-TIME PIC 9(8). 05 ELAPSED-TIME PIC 9(8). 05 RECORDS-PROCESSED PIC 9(6). PROCEDURE DIVISION. ADVANCED-SORTING-MAIN. DISPLAY 'Advanced Sorting Techniques Demonstration' DISPLAY '========================================' *> Initialize large dataset PERFORM INITIALIZE-LARGE-DATASET *> Demonstrate different sorting algorithms PERFORM DEMONSTRATE-SORTING-ALGORITHMS *> Demonstrate performance comparison PERFORM DEMONSTRATE-PERFORMANCE-COMPARISON *> Demonstrate external sorting concepts PERFORM DEMONSTRATE-EXTERNAL-SORTING STOP RUN. INITIALIZE-LARGE-DATASET. *> Initialize large dataset for sorting DISPLAY 'Initializing Large Dataset:' PERFORM VARYING INIT-INDEX FROM 1 BY 1 UNTIL INIT-INDEX > 100 MOVE INIT-INDEX TO RECORD-ID(INIT-INDEX) STRING 'Record ' DELIMITED BY SIZE INIT-INDEX DELIMITED BY SIZE ' Data' DELIMITED BY SIZE INTO RECORD-DATA(INIT-INDEX) END-STRING COMPUTE RECORD-VALUE(INIT-INDEX) = INIT-INDEX * 10.50 END-PERFORM MOVE 100 TO RECORDS-PROCESSED DISPLAY 'Large dataset initialized with ' RECORDS-PROCESSED ' records' DISPLAY ' '. DEMONSTRATE-SORTING-ALGORITHMS. *> Demonstrate different sorting algorithms DISPLAY 'Sorting Algorithms Demonstration:' *> Test bubble sort SET BUBBLE-SORT TO TRUE PERFORM TEST-SORTING-ALGORITHM *> Test selection sort SET SELECTION-SORT TO TRUE PERFORM TEST-SORTING-ALGORITHM *> Test insertion sort SET INSERTION-SORT TO TRUE PERFORM TEST-SORTING-ALGORITHM DISPLAY ' '. TEST-SORTING-ALGORITHM. *> Test a specific sorting algorithm MOVE 0 TO COMPARISON-COUNT MOVE 0 TO SWAP-COUNT ACCEPT START-TIME FROM TIME IF BUBBLE-SORT PERFORM BUBBLE-SORT-ALGORITHM DISPLAY 'Bubble Sort Results:' ELSE IF SELECTION-SORT PERFORM SELECTION-SORT-ALGORITHM DISPLAY 'Selection Sort Results:' ELSE IF INSERTION-SORT PERFORM INSERTION-SORT-ALGORITHM DISPLAY 'Insertion Sort Results:' END-IF END-IF END-IF ACCEPT END-TIME FROM TIME COMPUTE ELAPSED-TIME = END-TIME - START-TIME DISPLAY ' Comparisons: ' COMPARISON-COUNT DISPLAY ' Swaps: ' SWAP-COUNT DISPLAY ' Time: ' ELAPSED-TIME ' milliseconds'. BUBBLE-SORT-ALGORITHM. *> Implement bubble sort algorithm PERFORM VARYING BUBBLE-I FROM 1 BY 1 UNTIL BUBBLE-I > RECORDS-PROCESSED - 1 PERFORM VARYING BUBBLE-J FROM 1 BY 1 UNTIL BUBBLE-J > RECORDS-PROCESSED - BUBBLE-I ADD 1 TO COMPARISON-COUNT IF RECORD-VALUE(BUBBLE-J) > RECORD-VALUE(BUBBLE-J + 1) PERFORM SWAP-LARGE-RECORDS ADD 1 TO SWAP-COUNT END-IF END-PERFORM END-PERFORM. SELECTION-SORT-ALGORITHM. *> Implement selection sort algorithm PERFORM VARYING SELECT-I FROM 1 BY 1 UNTIL SELECT-I > RECORDS-PROCESSED - 1 MOVE SELECT-I TO MIN-INDEX PERFORM VARYING SELECT-J FROM SELECT-I + 1 BY 1 UNTIL SELECT-J > RECORDS-PROCESSED ADD 1 TO COMPARISON-COUNT IF RECORD-VALUE(SELECT-J) < RECORD-VALUE(MIN-INDEX) MOVE SELECT-J TO MIN-INDEX END-IF END-PERFORM IF MIN-INDEX NOT = SELECT-I PERFORM SWAP-LARGE-RECORDS ADD 1 TO SWAP-COUNT END-IF END-PERFORM. INSERTION-SORT-ALGORITHM. *> Implement insertion sort algorithm PERFORM VARYING INSERT-I FROM 2 BY 1 UNTIL INSERT-I > RECORDS-PROCESSED MOVE RECORD-VALUE(INSERT-I) TO KEY-VALUE MOVE INSERT-I TO INSERT-J PERFORM UNTIL INSERT-J = 1 OR RECORD-VALUE(INSERT-J - 1) <= KEY-VALUE ADD 1 TO COMPARISON-COUNT MOVE RECORD-VALUE(INSERT-J - 1) TO RECORD-VALUE(INSERT-J) SUBTRACT 1 FROM INSERT-J END-PERFORM MOVE KEY-VALUE TO RECORD-VALUE(INSERT-J) END-PERFORM. SWAP-LARGE-RECORDS. *> Swap two large records MOVE RECORD-ID(BUBBLE-J) TO TEMP-RECORD-ID MOVE RECORD-DATA(BUBBLE-J) TO TEMP-RECORD-DATA MOVE RECORD-VALUE(BUBBLE-J) TO TEMP-RECORD-VALUE MOVE RECORD-ID(BUBBLE-J + 1) TO RECORD-ID(BUBBLE-J) MOVE RECORD-DATA(BUBBLE-J + 1) TO RECORD-DATA(BUBBLE-J) MOVE RECORD-VALUE(BUBBLE-J + 1) TO RECORD-VALUE(BUBBLE-J) MOVE TEMP-RECORD-ID TO RECORD-ID(BUBBLE-J + 1) MOVE TEMP-RECORD-DATA TO RECORD-DATA(BUBBLE-J + 1) MOVE TEMP-RECORD-VALUE TO RECORD-VALUE(BUBBLE-J + 1). DEMONSTRATE-PERFORMANCE-COMPARISON. *> Demonstrate performance comparison DISPLAY 'Performance Comparison:' DISPLAY 'Algorithm Comparisons Swaps Time' DISPLAY '----------------------------------------' DISPLAY 'Bubble Sort: ' COMPARISON-COUNT ' ' SWAP-COUNT ' ' ELAPSED-TIME DISPLAY 'Selection: ' COMPARISON-COUNT ' ' SWAP-COUNT ' ' ELAPSED-TIME DISPLAY 'Insertion: ' COMPARISON-COUNT ' ' SWAP-COUNT ' ' ELAPSED-TIME DISPLAY ' '. DEMONSTRATE-EXTERNAL-SORTING. *> Demonstrate external sorting concepts DISPLAY 'External Sorting Concepts:' DISPLAY 'External sorting is used for large datasets that' DISPLAY 'cannot fit entirely in memory. Key concepts include:' DISPLAY '1. Divide data into smaller chunks' DISPLAY '2. Sort each chunk individually' DISPLAY '3. Merge sorted chunks into final result' DISPLAY '4. Use temporary files for intermediate results' DISPLAY '5. Optimize I/O operations for performance' DISPLAY ' ' DISPLAY 'COBOL SORT statement handles external sorting automatically' DISPLAY 'when data exceeds available memory.'

This example demonstrates advanced sorting techniques including different sorting algorithms (bubble sort, selection sort, insertion sort), performance comparison, and external sorting concepts. The program shows how to implement various sorting algorithms, measure their performance characteristics, and understand the trade-offs between different sorting methods. External sorting concepts for handling large datasets are also explained.

Collating Sequences and Ordering

1. Collating Sequence Impact

Collating sequences significantly impact data ordering by determining the order of characters in sorted output. Different collating sequences (EBCDIC, ASCII, custom) produce different sort orders for the same data. Understanding collating sequence impact is crucial for ensuring consistent data ordering across different systems and environments.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
IDENTIFICATION DIVISION. PROGRAM-ID. COLLATING-SEQUENCE-ORDERING. *> This program demonstrates collating sequence impact on ordering DATA DIVISION. WORKING-STORAGE SECTION. *> Collating sequence demonstration data 01 COLLATING-DATA. 05 COLLATING-ARRAY OCCURS 10 TIMES. 10 COLLATING-VALUE PIC X(10). 01 COLLATING-CONTROLS. 05 COLLATING-TYPE PIC X(1). 88 EBCDIC-COLLATING VALUE 'E'. 88 ASCII-COLLATING VALUE 'A'. 88 CUSTOM-COLLATING VALUE 'C'. 05 COLLATING-ORDER PIC X(1). 88 ASCENDING-ORDER VALUE 'A'. 88 DESCENDING-ORDER VALUE 'D'. 01 COLLATING-RESULTS. 05 SORT-COMPARISONS PIC 9(4). 05 COLLATING-STATUS PIC X(1). 88 COLLATING-SUCCESS VALUE 'S'. 88 COLLATING-FAILURE VALUE 'F'. PROCEDURE DIVISION. COLLATING-SEQUENCE-MAIN. DISPLAY 'Collating Sequence Ordering Demonstration' DISPLAY '=========================================' *> Initialize collating test data PERFORM INITIALIZE-COLLATING-DATA *> Demonstrate EBCDIC collating sequence PERFORM DEMONSTRATE-EBCDIC-COLLATING *> Demonstrate ASCII collating sequence PERFORM DEMONSTRATE-ASCII-COLLATING *> Demonstrate custom collating sequence PERFORM DEMONSTRATE-CUSTOM-COLLATING STOP RUN. INITIALIZE-COLLATING-DATA. *> Initialize test data for collating sequence demonstration DISPLAY 'Initializing Collating Test Data:' MOVE 'Apple' TO COLLATING-VALUE(1) MOVE 'banana' TO COLLATING-VALUE(2) MOVE 'Cherry' TO COLLATING-VALUE(3) MOVE 'date' TO COLLATING-VALUE(4) MOVE 'Elderberry' TO COLLATING-VALUE(5) MOVE 'fig' TO COLLATING-VALUE(6) MOVE 'Grape' TO COLLATING-VALUE(7) MOVE 'honeydew' TO COLLATING-VALUE(8) MOVE 'Kiwi' TO COLLATING-VALUE(9) MOVE 'lemon' TO COLLATING-VALUE(10) DISPLAY 'Test data initialized with mixed case strings' DISPLAY ' '. DEMONSTRATE-EBCDIC-COLLATING. *> Demonstrate EBCDIC collating sequence ordering DISPLAY 'EBCDIC Collating Sequence Ordering:' DISPLAY 'EBCDIC typically orders: A-Z, then a-z' SET EBCDIC-COLLATING TO TRUE SET ASCENDING-ORDER TO TRUE PERFORM SORT-BY-COLLATING-SEQUENCE DISPLAY 'EBCDIC Sort Order:' PERFORM DISPLAY-COLLATING-RESULTS DISPLAY ' '. DEMONSTRATE-ASCII-COLLATING. *> Demonstrate ASCII collating sequence ordering DISPLAY 'ASCII Collating Sequence Ordering:' DISPLAY 'ASCII typically orders: A-Z, then a-z' SET ASCII-COLLATING TO TRUE SET ASCENDING-ORDER TO TRUE PERFORM SORT-BY-COLLATING-SEQUENCE DISPLAY 'ASCII Sort Order:' PERFORM DISPLAY-COLLATING-RESULTS DISPLAY ' '. DEMONSTRATE-CUSTOM-COLLATING. *> Demonstrate custom collating sequence ordering DISPLAY 'Custom Collating Sequence Ordering:' DISPLAY 'Custom sequence: case-insensitive alphabetical' SET CUSTOM-COLLATING TO TRUE SET ASCENDING-ORDER TO TRUE PERFORM SORT-BY-CUSTOM-COLLATING DISPLAY 'Custom Sort Order (Case-Insensitive):' PERFORM DISPLAY-COLLATING-RESULTS DISPLAY ' '. SORT-BY-COLLATING-SEQUENCE. *> Sort by collating sequence MOVE 0 TO SORT-COMPARISONS PERFORM VARYING COLLATE-I FROM 1 BY 1 UNTIL COLLATE-I > 9 PERFORM VARYING COLLATE-J FROM COLLATE-I + 1 BY 1 UNTIL COLLATE-J > 10 ADD 1 TO SORT-COMPARISONS IF EBCDIC-COLLATING PERFORM EBCDIC-COMPARISON ELSE IF ASCII-COLLATING PERFORM ASCII-COMPARISON END-IF END-IF END-PERFORM END-PERFORM. EBCDIC-COMPARISON. *> EBCDIC collating sequence comparison IF ASCENDING-ORDER IF COLLATING-VALUE(COLLATE-I) > COLLATING-VALUE(COLLATE-J) PERFORM SWAP-COLLATING-RECORDS END-IF ELSE IF COLLATING-VALUE(COLLATE-I) < COLLATING-VALUE(COLLATE-J) PERFORM SWAP-COLLATING-RECORDS END-IF END-IF. ASCII-COMPARISON. *> ASCII collating sequence comparison IF ASCENDING-ORDER IF COLLATING-VALUE(COLLATE-I) > COLLATING-VALUE(COLLATE-J) PERFORM SWAP-COLLATING-RECORDS END-IF ELSE IF COLLATING-VALUE(COLLATE-I) < COLLATING-VALUE(COLLATE-J) PERFORM SWAP-COLLATING-RECORDS END-IF END-IF. SORT-BY-CUSTOM-COLLATING. *> Sort by custom collating sequence (case-insensitive) MOVE 0 TO SORT-COMPARISONS PERFORM VARYING COLLATE-I FROM 1 BY 1 UNTIL COLLATE-I > 9 PERFORM VARYING COLLATE-J FROM COLLATE-I + 1 BY 1 UNTIL COLLATE-J > 10 ADD 1 TO SORT-COMPARISONS PERFORM CUSTOM-COMPARISON END-PERFORM END-PERFORM. CUSTOM-COMPARISON. *> Custom collating sequence comparison (case-insensitive) MOVE FUNCTION UPPER-CASE(COLLATING-VALUE(COLLATE-I)) TO TEMP-VALUE-1 MOVE FUNCTION UPPER-CASE(COLLATING-VALUE(COLLATE-J)) TO TEMP-VALUE-2 IF ASCENDING-ORDER IF TEMP-VALUE-1 > TEMP-VALUE-2 PERFORM SWAP-COLLATING-RECORDS END-IF ELSE IF TEMP-VALUE-1 < TEMP-VALUE-2 PERFORM SWAP-COLLATING-RECORDS END-IF END-IF. SWAP-COLLATING-RECORDS. *> Swap two collating records MOVE COLLATING-VALUE(COLLATE-I) TO TEMP-COLLATING-VALUE MOVE COLLATING-VALUE(COLLATE-J) TO COLLATING-VALUE(COLLATE-I) MOVE TEMP-COLLATING-VALUE TO COLLATING-VALUE(COLLATE-J). DISPLAY-COLLATING-RESULTS. *> Display collating sequence results PERFORM VARYING DISPLAY-INDEX FROM 1 BY 1 UNTIL DISPLAY-INDEX > 10 DISPLAY ' ' DISPLAY-INDEX ': ' COLLATING-VALUE(DISPLAY-INDEX) END-PERFORM DISPLAY ' Comparisons made: ' SORT-COMPARISONS.

This example demonstrates the impact of collating sequences on data ordering, showing how different collating sequences (EBCDIC, ASCII, custom) produce different sort orders for the same data. The program implements case-sensitive and case-insensitive sorting, showing how collating sequences affect character comparison and sorting results. Custom collating sequences for specialized ordering requirements are also demonstrated.

Best Practices for Data Ordering

Common Data Ordering Patterns