Page 1 of 1

Sort in specified order other than ascending or descending?

PostPosted: Wed Oct 10, 2012 4:01 am
by BryPri55
Is there a way to use DSORT to sort based on specified values of a field, rather than in ascending or descending order? Say I had a file with records
02Smith
02Johnson
05Davis
06Thompson
02Jones
03Jackson
05Williams
07Newman

and I needed the records with 05 and 06 in the first two bytes to be in the file before all other records. How could I accomplish that?

Re: Sort in specified order other than ascending or descendi

PostPosted: Wed Oct 10, 2012 5:41 am
by BillyBoyo
Yes, you can. Extend your record by another sort key of one byte. Use IFTHEN to set that to 3 for 05's and 06's otherwise 7. Sort on the one-byte then the two-byte. Should give you what you want.

Where you extend the record depends on whether F or V.

I've used 3 and 7 so there is "space" for other future rearrangement :-) Doesn't have to be a numeric value. If you are unclear, please let us know. If you are clear, please post the solution as it may help others with similar requirements.

Re: Sort in specified order other than ascending or descendi

PostPosted: Wed Oct 10, 2012 7:16 am
by BryPri55
Thank you! I definitely understand what you are telling me I need to do. I don't have a lot of experience using DSORT with anything other than very elementary sorts and selects, however, so I'm not sure what the syntax would look like for extending a file by a byte using IFTHEN. The file I am using is fixed block. If you have time to post a simple example of extending a record by a byte with an IFTHEN statement, that would be appreciated. In the meantime I will look for examples in this forum and online.

Re: Sort in specified order other than ascending or descendi

PostPosted: Wed Oct 10, 2012 8:08 am
by dick scherrer
Hello and welcome to the forum,

This previous topic is similar to what you are looking for:
dfsort-icetool-icegener/topic6889.html#p30562

You will be most interested in Frank's solution at the end of the topic.

Re: Sort in specified order other than ascending or descendi

PostPosted: Wed Oct 10, 2012 9:00 pm
by skolusu
BryPri55,

Apart from adding an indicator at the end, you can use ALTSEQ which character EBCDIC, with alternate collating sequence, unsigned. This is similar to format CH, but the characters collate according to the ALTSEQ (alternate collating sequence) table in effect. In your case we are dealing with only numbers in first 2 bytes. So we will change the value of 5(X'F5') to A(X'CA') and 6(X'F6') to B(X'CB') and perform the sort. Note that this will only effect the sorting but contents of your records remain the same.

Use the following DFSORT JCL
//STEP0010 EXEC PGM=SORT               
//SYSOUT   DD SYSOUT=*                 
//SORTIN   DD *                       
02SMITH                               
02JOHNSON                             
05DAVIS                               
06THOMPSON                             
02JONES                               
03JACKSON                             
05WILLIAMS                             
07NEWMAN                               
//SORTOUT  DD SYSOUT=*                 
  SORT FIELDS=(1,2,AQ,A),EQUALS       
  ALTSEQ CODE=(F5CA,F6CB)             
//*


will produce
05DAVIS       
05WILLIAMS   
06THOMPSON   
02SMITH       
02JOHNSON     
02JONES       
03JACKSON     
07NEWMAN     

Re: Sort in specified order other than ascending or descendi

PostPosted: Thu Oct 11, 2012 3:53 am
by BryPri55
Awesome! I was able to produce the results I needed using both methods. Thanks for the great posts.