• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Want a Fast and Easy Way To Sort Subfile Data?

    October 8, 2008 Susan Gantner

    Note: The code accompanying this article is available for download here.

    Sorting data is something RPG programs often need to do. If it’s just a simple single field array you’re sorting in order to use the much faster binary search possible with %Lookup, for example, then SORTA works well and is simple. But what if it is a more complex task like sorting the data in a subfile on a user-selected column? Surely you need some more involved techniques, such as retrieving the data from the database again using a different ORDER BY on an SQL SELECT statement or using a different logical file or you could use the qsort C function for sorting the array elements in the program. Something as simple as SORTA can’t be used for that, right?

    Maybe so. The circumstances where this is effective are limited, for sure, but if your requirements fit, then using SORTA with a group field can be the simplest way and often a faster alternative than other methods you may have tried.

    First of all, what’s a group field? It’s a field in a data structure that is broken down into smaller subfields. For example, group field SflData might be made up of information about products (name, price, quantity) by using the Overlay keyword, such as:

      D  SflDS          Ds                  Inz
      D  SflData                            Like(SflRecData)
      D                                     Dim(999)
      D   Name                              Like(ProdDS)
      D                                     Overlay(SflData)
      D   Price                             Like(SellPr)
      D                                     Overlay(SflData:*Next)
      D   Qty                               Like(STOH)
      D                                     Overlay(SflData:*Next)
    

    The effect is similar to nested data structures, except without the requirement to use qualified names. (Likewise, there are many limitations on group fields because of the lack of name qualification.) One additional thing that’s nice about group fields compared to nested DSs is that we can use SORTA against any of the subfields in a group field array.

    So this means if I wanted to sort the data in the SflData array by product name, I could do that with the following statement: SortA Name;. Much simpler than any of those other options I mentioned above! Of course, in nearly all cases, it would require the use of the built-in function %SubArr (substring array) because I’m not likely to have filled up all 999 elements of SflData. Even so, the entire bit of logic to accomplish sorting this subfile data in the sequence of any of the three fields could be as simple as:

            If SortByName;
               SortA %SubArr(Name:1:Count);
            ElseIf SortByQty;
               SortA %SubArr(Qty:1:Count);
            ElseIf SortByPrice;
               SortA %SubArr(Price:1:Count);
            EndIf;
    

    This technique is very simple and in most cases quite a fast way to sort subfile data (or any other kind of repeating data). It does have significant limitations. For example, you can only sort on one subfield at a time. (Of course, you could group two subfields together if they happen to be adjacent in the subfile record.) Also, you must be able to retrieve and store all the data destined for the subfile into an array so that you can sort it all together. For some very large subfiles, that won’t be practical. But for those occasions where it works, it couldn’t get much simpler.

    You also need to think about the timeliness of the data since by using this technique you are not re-retrieving the data on each sort request, so there is no chance for recent record additions or updates to be reflected. This point could be considered either positive or negative. In some cases, the users may actually prefer that to see the same data without update in different sequences.

    The process works something like this. You retrieve the data destined for the subfile and store it in the group field array, such as the one described above, keeping a count of the number of elements you have loaded. (In my example, this is stored in Count.) Then you load the actual subfile from the array. Those steps could be combined into one process for the initial subfile load if you prefer. Also, you can decide for yourself whether you want to load the entire subfile from the array at once or use the page at a time approach. However, in order for the simple SORTA approach to work, you must have all the subfile data loaded into the array, so you won’t save as much time with the page at a time subfile load as you would otherwise.

    If/when the user requests a different sequence for the data, simply sort the group field array using logic similar to that shown above and then clear and refill the subfile from the array. I’ve found this to be a very fast technique for simple sorts of subfile data of a reasonable size.

    Of course, there is no reason at all to limit this sorting technique to subfile data. It could be used for any similar sorting requirement. I’ve had many requests over the years for “the best way to accomplish user-controlled sequencing of subfile data” and this is one very simple option.

    Here are a few things to keep in mind if you want to try it. Remember that the DIM keyword goes on the group field, not on the subfields that you will be sorting on. Also don’t forget that you will almost certainly need to use %SubArr to ensure you don’t get all the “empty” elements appearing first in your subfile.

    I find it simpler to make the group field an exact duplicate of the subfile record, so that’s why I define my group field to be like a data structure that is created as either an externally described DS or a LIKEREC DS. In my simple example, I made every subfile field sortable. In real life, of course, this is not usually desirable. Since my group field is a duplicate of the subfile output record, it simplifies the logic of writing to the subfile.

    In my example program, I’ve avoided using either pointers or qualified data names to keep the logic more understandable to a wider audience. As a result, of course, the logic is not quite as efficient as it could have been. I do, however, write to the subfile using a DS in the result field on the Write operation because the logic to load each field would be far too cumbersome. My logic to fill the subfile looks like the following (note that SflRecData is my externally described DS based on the output format of the subfile record):

        FOR RRN = 1 to Count;
    
            SflRecData = SflData(RRN);
            WRITE ProdSfl SflRecData;
    
        ENDFOR;                           
    

    If you want to see the completed code for my very simple example program, you can view the full program code here.

    Susan Gantner is one of the most respected System i gurus in the world and is one of the co-founders of System i Developer, an organization dedicated to RPG, DB2, and other relevant software technologies for the System i platform that hosts the new RPG & DB2 Summit conference. Gantner, who has worked in IBM’s Rochester and Toronto labs, left IBM to focus on training OS/400 and i5/OS shops on the latest programming technologies. She is also a regular speaker at COMMON and other user groups. Send your questions or comments for Susan to Ted Holt via the IT Jungle Contact page.



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Computer Measurement Group:  CMG '08 International Conference, December 7-12, Las Vegas
    looksoftware:  snap the best back-end into the coolest front-end
    Vision Solutions:  A $20 gas card for completing a short i5/OS DR survey

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    Getting Started with PHP for i5/OS: List Price, $59.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    LogiXML Delivers New Data Visualization Tool, Called VizLytics IBM Doubles the Cores on Midrange Power Systems

    Leave a Reply Cancel reply

Volume 8, Number 34 -- October 8, 2008
THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
ARCAD Software

Table of Contents

  • Want a Fast and Easy Way To Sort Subfile Data?
  • Testing for Valid Data Representation in SQL
  • Admin Alert: When System Job Tables Attack, Part III

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle