Sorting data in RPGLE

2920 pts.
RPG arrays
RPGLE Program
How to sort data in rpgle program countrywise in program coding only?

Answer Wiki

Thanks. We'll let you know when a new response is added.

Data — in — the program can be sorted if it’s in an array
I would make an array of long char fields placing the fields to sort on at the beginning. Sort the array and then you can use it.

But usually we fetch the data in the order we need it in ..through logical files, views, or SQL statements.


Discuss This Question: 2  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Sloopy
    If all you want to sort is a list of country codes, then an array with elements the size of the country code will do it. Of course, if you sort the array in ascending order, any blank entries will come first. You could get round that by initialising the array to *HIVAL; then, when you read through the array, you will stop when you reach a *HIVAL entry (or the end of the array). If you want to sort connected data items, for example, a country code and its country name, you can do that too, using an RPG 'trick'. See it here: As has been stated, normally data would be brought into the program in the 'right' order using logical files or views. But this is not always possible or desireable. Where I work, we have batch programs which use standing data such as country information. We would not want to chain to the country file every time we wanted that data - it would add a huge number of reads, even though there is only a small number of countries. So, we read the country data into arrays in the program during initialisation. Then we can do a LOOKUP whenever we want a country. In that case, we don't need to sort the array because the data is coming in in country code order anyway. But there is an additional 'trick' for speeding up array access, particularly if you don't know how many unused entries you may have. That is to load the array from the last element, backwards. You read the data back-keyed too, using SETGT and READP. So, if you have an array with a capacity of 200 elements, and only read 70, when you do a lookup you start the lookup at the 131st. element (you keep that value after loading). This means you miss out all the blank elements and the look-up is quicker. I am assuming here that all the arrays used for lookups have the ASCENDING property. The impact on long-running batch jobs of bringing often-used data into arrays can be substantial - it could even get you a pay rise.... Regards, Sloopy
    2,195 pointsBadges:
  • Sloopy
    Yet again, this text editor is playing games. The link to the answer on subfield arrays is here : :-~
    2,195 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: