I am trying to sort a file based on below mentioned fields, file length = 145
Code:
SORT FIELDS=(81,1,CH)
( default ascending)
Column 81 data is mentioned below for 10 RECORDS
Code:
A
C
Z
H
Y
M
Y
D
B
H
The output gives me the sorted data as based on column 81
Code:
A
B
C
D
H
H
M
Y
Y
Z
I need to get the output like
Code:
A
B
C
D
H
Y
Y
H
M
Z
Sorted for all except dumps 'H' and 'Y' as they come exactly from the first instance of eithers' occurrence is there a way to elegantly use sort to get this in a simple way .
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
OK, tested with your one-byte keys as the entire record:
Code:
* ENSURE THAT INPUT ORDER IS PRESERVED FOR DUPLICATE KEYS
*
OPTION EQUALS
* OUTPUT RECORD LENGTH FROM IFTHEN PROCESSING TO BE 2
*
INREC IFOUTLEN=2,
* TREAT H,Y AS THE START OF A ONE-RECORD GROUP - TO KNOW THE
* FIRST OF THE H,Y WHEN ID=01
*
IFTHEN=(WHEN=GROUP,BEGIN=(1,1,SS,EQ,C'H,Y'),
PUSH=(2:1,1,ID=2),RECORDS=1),
* A SECOND GROUP, TO PLACE THE SORT-KEY OF THE ID=01 ON ALL SUBSEQUENT
* RECORDS AS AN "OUTSORT"
*
IFTHEN=(WHEN=GROUP,
BEGIN=(3,2,CH,EQ,C'01'),
PUSH=(2:2,1)),
* FOR THOSE WHICH ARE NOT H,Y, OVERWRITE THE OUTSORT FIELD TO GET
* THE NORMAL ORDER FOR THOSE RECORDS
*
IFTHEN=(WHEN=(1,1,SS,NE,C'H,Y'),
OVERLAY=(2:1,1))
SORT FIELDS=(2,1,CH,A)
* CUT THE OUTSORT KEY OFF THE RECORD (THIS WOULD BE 1,1, BUT IS
* 1,2 TO SHOW THE OUTSORT KEY)
*
OUTREC BUILD=(1,2)
Thanks, will come back on the process mentioned by you by Tuesday
As for your queries ,
I assume that whichever of H or Y comes first, all the H and Y are to be sorted at that position?
[Abhishek] -- Actually if you see the results shown below you would understand how it was required .
Elegant? You mean code that is easy to understand?
[Abhishek] -- I meant simple and small , not involving too many functions
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
Well, happy if you are. Please describe your requirements better if there is a next time.
So Y is always to be sorted as H. Although we can't see that from your results. Even the "outsort" becomes ludicrously simple, but the ALTSEQ is more elegant.