|
|
![]() |
|
|
|
|
||
|
Properly Sorting Numeric Arrays Hey, Ted: In a program, data could be stored in arrays related to each other. Arrays could be filled from different files, but they could have the same index.
To sort the arrays, we would have to combine the arrays into one alphanumeric array and sort it. However, if the sort field is numeric, and there are negative numbers in the sort field, this is not going to work. I have a technique that allows proper sorting of numeric values in cases like this one. Would you like to publish it in Midrange Guru? -- A Devoted Reader I have good news for you. RPG knows how to handle this type of sorting. The short program that follows illustrates this technique. Notice that the array called MyArray is defined within a data structure. Two other subfields--Alpha and Numeric--overlay MyArray. All three subfields--MyArray, Alpha, and Numeric--are arrays. Sorting one keeps the others in sync. The >EVAL lines between the C specs were cut and pasted from the debugger to show what the values of the arrays were at that point. -- Ted
D ds
D MyArray 15 dim(5)
D Alpha 10 overlay(MyArray:1)
D Numeric 5s 0 overlay(MYArray:11)
C eval MyArray (1) = 'A.........00000'
C eval MyArray (2) = 'C.........50005'
C eval MyArray (3) = 'E.........2000K'
C eval MyArray (4) = 'D.........7000P'
C eval MyArray (5) = 'B.........40004'
>EVAL myarray
MYARRAY OF (1) = 'A.........00000'
MYARRAY OF (2) = 'C.........50005'
MYARRAY OF (3) = 'E.........2000K'
MYARRAY OF (4) = 'D.........7000P'
MYARRAY OF (5) = 'B.........40004'
C sorta Alpha
>EVAL myarray
MYARRAY OF (1) = 'A.........00000'
MYARRAY OF (2) = 'B.........40004'
MYARRAY OF (3) = 'C.........50005'
MYARRAY OF (4) = 'D.........7000P'
MYARRAY OF (5) = 'E.........2000K'
>EVAL numeric
NUMERIC OF (1) = 00000.
NUMERIC OF (2) = 40004.
NUMERIC OF (3) = 50005.
NUMERIC OF (4) = -70007.
NUMERIC OF (5) = -20002.
C sorta Numeric
>EVAL myarray
MYARRAY OF (1) = 'D.........7000P'
MYARRAY OF (2) = 'E.........2000K'
MYARRAY OF (3) = 'A.........00000'
MYARRAY OF (4) = 'B.........40004'
MYARRAY OF (5) = 'C.........50005'
>EVAL numeric
NUMERIC OF (1) = -70007.
NUMERIC OF (2) = -20002.
NUMERIC OF (3) = 00000.
NUMERIC OF (4) = 40004.
NUMERIC OF (5) = 50005.
C eval *inlr = *on
|
Editors
Contact the Editors |
|
Last Updated: 9/13/02 Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |