Blog Archive

RPGLE Sort - Using SORTA, C Library qsort function

Sorting is any process of arranging items in some sequence and/or in different sets, and accordingly, it has two common, yet distinct meanings:
  1. ordering: arranging items of the same kind, class, nature, etc. in some ordered sequence,
  2. categorizing: grouping and labeling items with similar properties together (by sorts).

To sort data in RPGLE you have to populate an Array and then use the sortA op-code. But if you are looking to sort data structure arrays based on more than one sub-field then you need the C library qsort fucntion.


SORTA (Sort an Array)

Free-Form Syntax SORTA array-name
SORTA %SUBARR(array-name : start-element { : number-of-elements })

The array-name operand is the name of an array to be sorted. The array is sorted into sequence (ascending or descending), depending on the sequence specified for the array on the definition specification. If no sequence is specified, the array is sorted into ascending sequence. The array *IN cannot be specified.. If the array is defined as a compile-time or prerun-time array with data in alternating form, the alternate array is not sorted. Only the array specified as array-name is sorted.
If the array is defined with the OVERLAY keyword, the base array will be sorted in the sequence defined by the OVERLAY array.

Graphic arrays will be sorted by the hexadecimal values of the array elements, disregarding the alternate collating sequence, in the order specified on the definition specification.

To sort a portion of an array, use the %SUBARR built-in function.

Sample RPGLE Code using sortA op-code

d $data_count     s             10i 0                              
d i               s             10i 0                              
                                                                   
d                 ds                                               
d Record                        40a   Dim(6) Ascend                
d  First                        10a   Overlay(Record)              
d  Second                       10a   Overlay(Record:*Next)        
d  Third                        10a   Overlay(Record:*Next)        
d  Fourth                       10a   Overlay(Record:*Next)        
                                                                   
 /free                                                             
                                                                   
    Record(1) = 'A100000000B600000000A123456789A123456789';        
    Record(2) = 'C100000000B500000000A123456789A123456789';        
    Record(3) = 'B100000000B400000000A123456789A123456789';        
    Record(4) = 'D100000000B300000000A123456789A123456789';        
    Record(5) = 'F100000000B200000000A123456789A123456789';        
    Record(6) = 'E100000000B100000000A123456789A123456789';        
                                                                   
     for i = 1 to 6;                                               
         dsply Record(i);                                          
     endfor;                                                       
                                                                   
     // %elem will give you all the elements in the array           
     // this would include the blank ones.                          
     $data_count = %elem(Record);                                   
         dsply $data_count;                                         
     SortA %SubArr(second:1:$data_count);                           
                                                                    
     for i = 1 to 6;                                                
         dsply Record(i);                                           
     endfor;                                                        
                                                                    
     *inlr = *on;                                                   
 /end-free              

Click here for next Chapter




No comments :

Post a Comment