fhg
Volume 8, Number 34 -- October 8, 2008

Want a Fast and Easy Way To Sort Subfile Data?

Published: October 8, 2008

by 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


Sponsored By
HELP/SYSTEMS

SEQUEL™ -- IBM® System i™ Business Intelligence Made Easy

                  · Easy to use by IT and end users
                  · Automated data access and display
                  · Complete BI package: reports, tables, key performance indicators, and dashboards
                  · System i-centric for real-time data analysis
                  · Multiple interface options: graphical, green-screen, browser
                  · Expert support and training

SEQUEL meets your System i data access and analysis needs.

http://www.helpsystems.com/400g


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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


 
The Four Hundred
Bytware Bought by Help/Systems and Audax

The Power Systems 570 i Edition Versus Big Windows Boxes

An Open Letter to i Shops from the Power Systems GM

System Performance Management Is Like Having Insurance

IDC and i: Next Time, Can You Talk to Some Real i Shops?

The Linux Beacon
Why Blade Servers Still Don't Cut It, and How They Might

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Microsoft Ponies Up Another $100 Million for Novell Linux

Mad Dog 21/21: Newtonian Economics

Two More Xeon-Based Galaxy Servers from Sun

Four Hundred Stuff
QJRN/400 Sniffs Out Fraud, One Journal Receiver at a Time

Databorough Beefs Up X-Analysis for Application Modernization

BCD's Presto Web Enablement Software Goes GA

IBM Promotes the i--iPhone, That Is

Valid Gets IBM Certification for i OS-Based Biometric System

Big Iron
For Some Customers, the Mainframe Is Green

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
September 20, 2008: Volume 10, Number 38

September 14, 2008: Volume 10, Number 37

September 7, 2008: Volume 10, Number 36

August 30, 2008: Volume 10, Number 35

August 23, 2008: Volume 10, Number 34

August 16, 2008: Volume 10, Number 33

The Windows Observer
Citrix Addresses Performance with XenApp 5

Server Buyers Shop Like It's 1999 in the Second Quarter

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Mad Dog 21/21: Newtonian Economics

Microsoft Does Something About Those SQL Injection Attacks

The Unix Guardian
What the Heck Is the Midrange, Anyway?

Overseas and Notebook Sales Offset Printer Declines for HP in Q3

Two More Xeon-Based Galaxy Servers from Sun

Mad Dog 21/21: Newtonian Economics

Intel's Nehalems to Star at IDF, AMD Pitches Shanghai

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
ARCAD Software


Printer Friendly Version


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

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Data Queues vs. MQ Series: Performance

Removing blanks from a CL Variable

XML

SQL "Hidden" Field

Java Messages

MQ Help Desired





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement