Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to sort an array in COBOL

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Rajiv Lochan De

New User


Joined: 27 Dec 2007
Posts: 2
Location: kolkata

PostPosted: Fri Jun 06, 2008 5:25 pm    Post subject: How to sort an array in COBOL
Reply with quote

Hi all,
How to sort an array?
My problem is :
There is an one dim array,
01 ITEM-VAR.
05 ITEM OCCURS 150 TIMES.
10 DEL-DATE PIC S9(09) COMP-3.
10 DEPT-NEBR PIC S9(04) COMP-3.
10 DISP PIC X(01).

I want to sort this table by DEL-DATE and then within a date by DISP.

If my input array is :

30/06/07 20 R
10/06/07 30 S
20/06/07 30 R
30/06/07 20 A
10/06/07 30 R
30/06/07 40 S
30/06/07 20 R

Then output array will be

10/06/07 30 R
10/06/07 30 S
20/06/07 30 R
30/06/07 20 A
30/06/07 20 R
30/06/07 20 R
30/06/07 40 S


Thanks,
Rajiv
Back to top
View user's profile Send private message

Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Jun 06, 2008 5:32 pm    Post subject:
Reply with quote

What is the date format, I can see that it is COMP-3 is it in yyyymmdd format or mmddyyyy or ddmmyyyy?
Back to top
View user's profile Send private message
Rajiv Lochan De

New User


Joined: 27 Dec 2007
Posts: 2
Location: kolkata

PostPosted: Fri Jun 06, 2008 5:36 pm    Post subject:
Reply with quote

Date format is yyyymmdd
Back to top
View user's profile Send private message
itdsen

New User


Joined: 20 Sep 2006
Posts: 23
Location: Chennai

PostPosted: Wed Jun 11, 2008 7:29 pm    Post subject: Reply to: how to sort an array
Reply with quote

Is it possible declare date field in comp3.

comp3 only used only with numeric.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Wed Jun 11, 2008 8:11 pm    Post subject: Re: Reply to: how to sort an array
Reply with quote

itdsen wrote:
Is it possible declare date field in comp3.

comp3 only used only with numeric.


You don't declare a date as comp-3, you declare a field as comp-3 and use it store a numeric value representing a date. 20080611 will fit into a pic s9(9) comp-3 field with no problems, COBOL doesn't know it's a date but the programmers do.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8117
Location: East Dubuque, Illinois, USA

PostPosted: Wed Jun 11, 2008 8:42 pm    Post subject: Reply to: how to sort an array
Reply with quote

In batch you can invoke the system sort via the SORT verb -- see manual for SD and so forth needed. Or, you can implement a sort algorithm from any college text book in your code.

In CICS you don't have access to system sort so you'll have to implement a sort algorithm against the array.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed Jun 11, 2008 8:46 pm    Post subject:
Reply with quote

Use this piece of code ....

Code:


01 SAVE-CODE1    PIC 9(9)  VALUE SPACES.
01 SAVE-CODE2    PIC S9(9) VALUE SPACES.
01 SAVE-CODE3    PIC X     VALUE SPACES.
01 S1           PIC S9(4) COMP VALUE 0.
01 S2           PIC S9(4) COMP VALUE 0.


01 ITEM-VAR.
    05 ITEM OCCURS 150 TIMES.
       10 DEL-DATE     PIC S9(09) COMP-3.
       10 DEPT-NEBR    PIC S9(04) COMP-3.
       10 DISP PIC     X(01).


PERFORM VARYING S1 FROM 1 BY 1 UNTIL S1 = 150
        PERFORM VARYING S2 FROM S1 BY 1 UNTIL S2 > 150
                IF DEL-DATE(S2) < DEL-DATE(S1)
                     MOVE DEL-DATE(S1)  TO SAVE-CODE1
                     MOVE DEL-DATE(S2)  TO DEL-DATE(S1)
                     MOVE SAVE-CODE1     TO DEL-DATE(S2)
                END-IF
        END-PERFORM
END-PERFORM.



this will sort del-date ... now perform for next variable or else treat the entire ITEM as a single string and sort .... as per ur requirement ...
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts XML Parse for COBOL 5.2 Bhanu Praveen COBOL Programming 1 Sat Jul 08, 2017 8:58 pm
No new posts SORT to unpack a feild shr_amar DFSORT/ICETOOL 7 Fri Jul 07, 2017 4:32 am
No new posts How to write Specific Fields from Mul... Padhu SYNCSORT 6 Thu Jul 06, 2017 10:26 am
No new posts Optimize sort inrec parse vishwakotin DFSORT/ICETOOL 6 Mon Jun 26, 2017 11:15 pm
No new posts Dynamic output file creation in cobol... smileheal COBOL Programming 7 Thu Jun 15, 2017 10:53 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us