Hi Guys,
If I may, can I suggest some alternative to this issue?
My suggestion is something that I've used very successfully several times in the past.
Basically, the solution is to store the data in the table as a Linked List data structure. This way, there is no need for a "Sort" process. Using an Internal Sort for 20 items is not very efficient, and not possible in a CICS environment.
The basic idea behind using a Singly-Linked-List data structure is that each occurrance in the table contains a "Pointer" to the Next Higher entry. The actual entries are stored in an Entry-Sequenced (Not Sorted) mode.
To support this process, you simply need to add one more field to the table:
10 WS-T1-POINTER PIC s9(3) comp-3.
Also you need to define a Starting Pointer, or "Root" in Working Storage. This Root pointer will point to the First entry in sequence.
01 ROOT-PTR PIC s9(3) comp-3 VALUE +0.
There are other support fields that you need to define. I won't go through them here, but the idea is that you keep adding new entries to the end of the entries, and just switch the pointers at the "Point of Insertion".
You would write a simple routine to ADD an entry into the table. Each segment of the program that needs to add data to the table will Perform the ADD-ENTRY routine. So, no matter where the entry is added, the ADD-ENTRY Routine will always insure that the pointers are adjusted to maintain a "Sorted" list based on the Pointer field of each entry. Note that at most, only two pointers (current and prev, if required) will need to be adjusted with each entry added to the table.
So whenever the program needs to add an entry to the table, it always adds the new entry at the end of the current entries by Performing the ADD-ENTRY routine to adjust the pointers and place the new entry at the end of the table.
The ADD-ENTRY routine will Traverse the Table, using the ROOT-PTR as the Starting location, and the WS-T1-POINTER to navigate to the NEXT entry, to find the place to "Insert" the new entry. Remember though, we aren't Really Inserting the entry there. Instead, we just flip the pointers. The new entry is in fact always added at the end of the table.
There are several kinds of Linked Lists, the one that I described is called a Singly Linked List. It's really an efficient way of maintaining an ordered list without the need to SORT the table. If you want to list the table entries in sorted sequence, simply traverse the table using the pointers from the root to the last entry.
Since your table is small (occurs 20), a singly linked list will do fine. If you had many entries, I would suggest a Binary Tree data structure, but for your purposes, I think the linked list is the way to go. Basically, Linked Lists are good for a small, finite set of entries.
Developing a Linked List data structure in COBOL is not very complicated, and is a great way of maintaining ordered lists.