Solved

QSORT apI

Posted on 2004-10-04
8
829 Views
Last Modified: 2008-01-16

Hello friends,
I have asked a question related to soring subfile which i got answer from daveslater .
But i couldn't understood how to use QSORT.
I have a subfile with values in status field like -
e.g  Open,Delete,Close in status field.
When the user types 1 - the subfile should be displayed records by - Open,Delete,Close.
Similarly  When the user types 2 - the subfile should be displayed records by - Delete,Close,Open.
i.e sorting status field on status field .
How can i use QSORT api in this situation ??
Do i need to pass the D,C,O paramter to this API procedure ??

Pls give an example.
Thanks and waiting..

Raj

0
Comment
Question by:raj28
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:daveslater
Comment Utility
What you are trying to achieve is grouping, to perform this you will need some hidden fields in the subfile and some logic to populate these fields

say the fields are called
SORT01
SORT02

The logic would be ((** notes commedns addes for clarity and will not cust and past)

IF Status = 'Open'
eval sort01 = '1'   ** sort by Open first
eval sort02 = '3' ** sort by open last

elseif ststus = "delete"
eval sort01 = '2' ** sort bu delete second
eval sort02 = '1' ** sort by delete first

elseif ststus = "close"
eval sort01 = '3'  ** sort by close third
eval sort02 = '2' ** sort by close 2nd

endif


write subfile


You can then sort by the hidden field to perform the grouping.

The more criteria you have the more hidden fields you have and the more comples the IF statements

I hope that is clear

dave


0
 

Author Comment

by:raj28
Comment Utility
Hi dave,
I understand a little bit but not more . Do i need to use QSORT api ?? How ??
Pls give example in detail ..as what u have given last time .

Thanks n waiting
Raj
0
 
LVL 14

Expert Comment

by:daveslater
Comment Utility
Hi raj28
can you give a bit more info on what you are trying to achieve; they may be a better way of doing it.
I have just started a new job; this involves a lot more meetings so for a few weeks I will have less time to help here (untill I can get my Broadband connection to the 400)

Dave
0
 

Author Comment

by:raj28
Comment Utility
Hi dave,
I couldn't understand how to use the hidden fields for sorting . Do u mean while loading the subfile - i have to move values to the hidden field (i.e if it is open then '1' , Close then '2' like this ) ?? If it is then how can i sort ?? Do i need to use QSORT api along with this ?? If yes then what are the parameters required for the procedure ??

I think u got my question . Thanks for your valuable time .

Raj..
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:daveslater
Comment Utility
Hi
It will be  Friday till I get a bit of time but if not one else posts domthing I will post the code then.

Dave
0
 
LVL 27

Expert Comment

by:tliotta
Comment Utility
raj28:

Just in case this part is unclear, you _can't_ use the hidden fields to sort by.

That is, you can't sort the subfile at all.

That is, you have to write the subfile records in the order you want them sorted in. Sort before writing to the subfile.

The use of a hidden field would be so that you can read the subfile records back into the program and pass them to a sort function before writing them back out in a different order. If you don't store a hidden field with the subfile record, you'll have to supply the sort key for every record every time you re-send the records through the sort.

In short, the subfile is never sorted.

Instead, you sort an array or a multi-occurrence data structure or a temporary physical file or some other data object; then you write the data to the subfile again.

For qsort, you would have the subfile records possibly stored in an array and pass the address of the array to qsort. (It could be a MODS or a *usrspc or just a block of memory.) Then you'd write the subfile from the array, one subfile record at a time.

If the user chose a different sort, you'd pass the array address to qsort again and write the subfile records again from the array after the sort finished again. The "hidden" fields would be written to the subfile only if you wanted to read them back in. They could only be part of the array and never written to the subfile if that's how you wanted to do it.

(I got a little concerned that sorting subfiles _might_ be misunderstood.)

Now, since sometimes you want the sort order to be D-C-O and other times you want it to be O-D-C, qsort can be a useful choice for sorting. You will create a routine that can tell qsort whether to sort [O] as the highest value or the lowest value.

In fact, qsort doesn't really "sort" at all. It takes two elements from your array and then calls your routine and your routine tells qsort which of the two elements is higher. Qsort then rearranges them in memory if necessary, and then picks two more elements for your routine to compare. This means that qsort simply arranges the elements in whatever order _YOUR_ routine chooses for every pair of elements.

Qsort is written in such a way that it chooses the best elements to compare so that the whole opertaion goes fast if your routine is fast. It does this while also allowing total flexibility in sort sequencing. And that makes it suitable for D-C-O/O-D-C sorting even though there's no obvious pattern; the pattern will exist in the routine logic that you have to write.

Tom
0
 

Author Comment

by:raj28
Comment Utility
Hi tiotta,
can u pls send me the code ??

thanks,
Raj
0
 
LVL 14

Accepted Solution

by:
daveslater earned 200 total points
Comment Utility
Here is an example, this used a secon program to\perform the sort.

****** VERY IMPORTANT COMPILE OPTION o# on display file *******


name sortaip
type DSPF
     A*****************************************************************
     A*
     A* System Name...  subfile position sort
     A*
     A* AUTHOR........  D.SLATER
     A*
     A* VERY IMPORTANT COMPILE OPTION
     A* SHARE(*YES)
     A*****************************************************************
     A                                      DSPSIZ(24 80 *DS3)
     A                                      CHGINPDFT(UL)
     A                                      MSGLOC(24)
     A                                      CA03
     A************************************************
     A          R F01SFL                    SFL
     A            SORT01         1   H
     A            SORT02         1   H
     A            FIELD01       20   O  6  4
     A************************************************
     A          R F01CTL                    SFLCTL(F01SFL)
     A                                      SFLSIZ(0016)
     A                                      SFLPAG(0015)
     A                                      OVERLAY
     A  31                                  SFLDSP
     A                                      SFLDSPCTL
     A N31                                  SFLCLR
     A  31                                  SFLEND(*MORE)
     A            RRN            3S 0H      SFLRCDNBR(CURSOR)
     A                                  4  4'Key'

................................................................................................................
name SORTAPI
TYpe RPGLE
     H
      *****************************************************************
      *
      * System Name...  DEMONSTRATE SUBFILE SORT
      *
      * AUTHOR........  D.SLATER
      *
      *****************************************************************
     Fsortapi   CF   E             WORKSTN SFILE(F01SFL:RRN)
     D RRNL            S                   LIKE(RRN)
     D Dta             S             20    dim(20) ctdata perrcd(1)
     D field01         ds
     d  opt                           1    overlay(Field01:1)
      ********************************************************************
      ** Load 20 records into the subfile
     c                   for       rrn=1 to %elem(dta)
     c                   eval      field01=dta(rrn)
      ** set sort order fields
     c                   If        Opt='O'
     c                   eval      sort01='1'                                   1'st
     c                   eval      sort02='3'                                   3'rd
     c                   elseif    Opt='C'
     c                   eval      sort01='2'                                   2'nd
     c                   eval      sort02='1'                                   1'st
     c                   elseif    Opt='D'
     c                   eval      sort01='3'                                   3'rd
     c                   eval      sort02='2'                                   2'nd
     c                   endif
     c
     c                   write     f01sfl
     c                   endfor
      * Set rrn firlds and subfile display
     c                   eval      rrnl=%elem(dta)
     c                   eval      *in31 = *on
     c                   eval      rrn=1
     c                   exfmt     f01ctl
      ** sort by seq1 and re-display
     C                   CALL      'SORTAPI1'
     C                   PARM                    RRNL
     C                   PARM      '1'           OPT               1
     c                   eval      *in31 = *on
     c                   eval      rrn=1
     c                   exfmt     f01ctl
      ** sort by seq 2 and re-display
     C                   CALL      'SORTAPI1'
     C                   PARM                    RRNL
     C                   PARM      '2'           OPT               1
     c                   eval      *in31 = *on
     c                   eval      rrn=1
     c                   exfmt     f01ctl
     c                   EVAL      *INLR=*ON
**    data to load subfile
Open    Record  1
Closed  Record  2
Deleted Record  3
Open    Record  4
Closed  Record  5
Deleted Record  6
Open    Record  7
Closed  Record  8
Deleted Record  9
Open    Record 10
Closed  Record 11
Deleted Record 12
Open    Record 13
Closed  Record 14
Deleted Record 15
Open    Record 16
Closed  Record 17
Deleted Record 18
Open    Record 19
Closed  Record 20



................................................................................................................
name SORTAPI1
TYpe RPGLE

     H  BNDDIR('QC2LE') dftactgrp(*No) actgrp(*caller)
     H OPTION(*NODEBUGIO)
      *****************************************************************
      *
      * System Name...  DEMONSTRATE SUBFILE SORT
      * This program accepts2 parms,the number of records in the subfile
      * and a sort seq. The subfile in the display file is then sorted
      *      into the requested order.
      *      
      * AUTHOR........  D.SLATER
      *
      *****************************************************************
      ** shared display file to enable re-write of records
     Fsortapi   CF   E             WORKSTN SFILE(F01SFL:RRN)
     D RRNL            S                   LIKE(RRN)
      ** MODS to enable sort - based on the subfile
     DSFLDS          e ds                  extname(sortapi:F01SFL)
     d                                     Occurs(9999)
     D seq             S              1
      ********************************************************************
      ** define Qsort to the program
     D qsort           PR                  ExtProc('qsort')
     D   base                          *   value
     D   num                         10U 0 value
     D   width                       10U 0 value
     D   compare                       *   procptr value
      ** Internal sort logic used by Qsort - sort seq 1
     DSORTPRA          PR            10i 0
     d   parm1                         *   value
     d   parm2                         *   value
      ** Internal sort logic used by Qsort - sort seq 2
     DSORTPRB          PR            10i 0
     d   parm1                         *   value
     d   parm2                         *   value
      ** Entry parms last rrn + sort option
     c     *entry        plist
     c                   Parm                    RRNL
     c                   Parm                    Seq
     c
      ** Load SFL into MODS
     c                   for       rrn=1 to rrnL
     c                   eval      %occur(sflds) = rrn
     c     rrn           chain     f01sfl
     c                   Endfor

      ** Sort the MODS into correct seq. prior to output
      ** Must set the MODS to index 1 before sort
     c                   eval      %occur(sflds) = 1
      ** When seq = 1 use logic in proc SORTPRA
     c                   if        Seq='1'
     c                   callp     qsort(%addr(sflds) : rrnl  :
     c                                   %size(sflds) : %paddr('SORTPRA'))
     c                   else
      ** When seq = 2 use logic in proc SORTPRB
     c                   callp     qsort(%addr(sflds) : rrnl  :
     c                                   %size(sflds) : %paddr('SORTPRB'))
     c                   endif
     c
      ** initialise the subfile and write sorted records
     c                   clear                   *in31
     c                   write     f01ctl
     c                   for       rrn =1 to rrnl
     c                   eval      %occur(sflds) = rrn
     c                   write     f01sfl
     c                   Endfor
      ** and exit to free resources
     c                   EVAL      *INLR=*ON
     C                   RETURN
      ***************************************************************************
      ** Procuedure used by QSORT to identify logic for sort
      ** This sorts only on the sort field so with-in each category
      ** the initial records are random
      ***************************************************************************
     PSORTPRA          B
     DSORTPRA          pi            10i 0
     D   parm1                         *   value
     D   parm2                         *   value
      ** Ds for first record
     D ds1           E ds                  based(parm1)
     D                                     extname(SORTAPI:F01SFL)
     D                                     PREFIX(P1)

      ** Ds for second record
     D ds2           E ds                  based(parm2)
     D                                     extname(SORTAPI:F01SFL)
     D                                     PREFIX(P2)
      **  Test conditions
     C                   IF        P1SORT01 <   P2SORT01
     C                   RETURN    -1
     C                   ELSEIF    P1SORT01 >   P2SORT01
     C                   RETURN    1
     C                   ELSE
     C                   RETURN    0
     C                   ENDIF
     C
     P                 E

      ***************************************************************************
      ** Procuedure used by QSORT to identify logic for sort
      ** This sorts using sort seq 2 and field001
      ** so that with-in each category the records in in the correct seq.
      ***************************************************************************
     PSORTPRB          B
     DSORTPRB          pi            10i 0
     D   parm1                         *   value
     D   parm2                         *   value
      ** Ds for first record
     D ds1           E ds                  based(parm1)
     D                                     extname(SORTAPI:F01SFL)
     D                                     PREFIX(P1)

      ** Ds for second record
     D ds2           E ds                  based(parm2)
     D                                     extname(SORTAPI:F01SFL)
     D                                     PREFIX(P2)
      **
     d p1s             s             +1    like(p1field01)
     d p2s             s             +1    like(p1field01)
      **  Test conditions
     c                   eval      p1s = P1SORT02 + p1field01
     c                   eval      p2s = P2SORT02 + p2field01

     C                   IF        P1S < p2s
     C                   RETURN    -1
     C                   ELSEIF    P1S > P2s
     C                   RETURN    1
     C                   ELSE
     C                   RETURN    0
     C                   ENDIF

     P                 E

..........................................................................





Dave
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now