|
<snip> What is the best method for sorting a subfile in ILE RPG. Can I use QSORT and somehow point to the subfile space? </snip> For those that have an interest, I have a utility (Service Program) called XVSRTQ which I will happy to provide. I can send to Leif at Think400 for download or can send directly. XVSRTQ was designed for just these situations. You can select any field or set of fields to sort on, and have then sorted using QSORT. I wrote it originally to be able to sort subfiles. The unique thing about XVSRTQ is that it deals with data types correctly. For example, it sorts negative number correctly and dates. A example of how it is used. In this example, a data structure mapped to a user space has four fields and I am going to sort on all four fields. First I load four records and then sort in ascending and then descending. Basically logic is to load record, build sort key and when loaded call sort. d cUsrSpcNam c const('TESTUSP') d cUsrSpcLib c const('QTEMP') d cText c const('Work User Space') d TestDs... d e ds ExtName(ASWORK) d Qualified d Occurs(32767) d Based(ptrTestDs) d W1XML4... d Like(StdDat) d SortKey... d 256 Varying d MessageId... d s Like(StdMsgId) d qMessageFileName... d s Like(StdQNam) d MessageData... d s 1024 Varying d x... d s Like(StdInt) /Free USPC_CreateUserSpace(cUsrSpcNam : cUsrSpcLib : %Size(TestDs): cTrue : cText : '*ALL' : '*YES' : 5 : '1' ); ptrTestDs = USPC_GetPointerToUserSpace(cUsrSpcNam: cUsrSpcLib); %Occur(TestDs) = 1; TestDs.W1XML1 = 'Rcd01'; TestDs.W1XML2 = 25.24; TestDs.W1XML3 = -15.905; TestDs.W1XML4 = %Date('1998-12-01'); Exsr srBuildSortKey; %Occur(TestDs) = 2; TestDs.W1XML1 = 'Rcd02'; TestDs.W1XML2 = -14.19; TestDs.W1XML3 = 16.70; TestDs.W1XML4 = %Date('1949-03-07'); Exsr srBuildSortKey; %Occur(TestDs) = 3; TestDs.W1XML1 = 'Rcd03'; TestDs.W1XML2 = -24.29; TestDs.W1XML3 = -55.25; TestDs.W1XML4 = %Date('1953-06-24'); Exsr srBuildSortKey; %Occur(TestDs) = 4; TestDs.W1XML1 = 'Rcd04'; TestDs.W1XML2 = -7.34; TestDs.W1XML3 = 12.25; TestDs.W1XML4 = %Date('2005-07-01'); Exsr srBuildSortKey; SRTQ_Sort(ptrTestDs : // Pointer to User Space. cSRTQ_Ascending : // Sort Ascending. 4 : // Number of records loaded. %Size(TestDs) : // Size of each record. %Size(TestDs.SortKey)); // Size of sort key. // Must be 256 or error. // Rebuild sort keys. Internal sort key is cleared by call to // SRTQ_Sort. Practically this means nothing. In a real system, // you would not sort one and then sort another. For x = 1 to 4; %Occur(TestDs) = x; Exsr srBuildSortKey; EndFor; SRTQ_Sort(ptrTestDs : // Pointer to User Space. cSRTQ_Descending : // Sort Descending. 4 : // Number of records loaded. %Size(TestDs) : // Size of each record. %Size(TestDs.SortKey)); // Size of sort key. For x = 1 to 4; %Occur(TestDs) = x; EndFor; Monitor; USPC_DeleteUserSpace(cUsrSpcNam: cUsrSpcLib); On-Error; ERRH_GetLastMessage(MessageId : QMessageFileName: MessageData ); If MessageId <> 'CPF2105'; ERRH_Throw(MessageId : QMessageFileName: MessageData ); EndIf; EndMon; *InLR = *On; Return; // Build Sort Key. BegSr srBuildSortKey; SRTQ_BuildSortKeyNumeric(cSRTQ_First : TestDs.W1XML2 : %Len(TestDs.W1XML2) : %DecPos(TestDs.W1XML2)); SRTQ_BuildSortKeyNumeric(cSRTQ_Next : TestDs.W1XML3 : %Len(TestDs.W1XML3) : %DecPos(TestDs.W1XML3)); SRTQ_BuildSortKeyAlpha(cSRTQ_Next : TestDs.W1XML1); SRTQ_BuildSortKeyDate(cSRTQ_Next : TestDs.W1XML4); TestDs.SortKey = SRTQ_GetSortKey(); EndSr;
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.