SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Defining dynamic orders in an ABAP sort



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Nov 25, 2007 11:00 pm    Post subject: Defining dynamic orders in an ABAP sort Reply with quote

Defining dynamic orders in an ABAP sort.

You can easily define dynamic fields in an ABAP sort using ( ) :

data : l_fieldname type dd03l-fieldname value 'CARRID'.
SORT sflight by (l_fieldname) DESCENDING.


BUT you CANNOT define the order (ascending or descending) dynamically:

SORT sflight by (l_fieldname) (l_order). <<==-- This is impossible in ABAP.


Now, with the new tree control?, when you want to give fully customizable tree, ou need to dynamically set the order in the sort command.

The first method is by using a CREATE SUBROUTINE POOL with your sort. Each time you will run the program, this subroutine will be recompiled. This is VERY slow and we should avoid using this command whenever it's possible.

The second method is a simple trick that have no major impact on performance. You duplicate each fields you want to sort, 1 copy for ascending and 1 for descending. You move the fieldname only in 1 of those fields depending on what order you want.

Here is a small example on how to do that:
Code:
*&---------------------------------------------------------------------*
*& Report ZDANY_DYN_SORT
*&---------------------------------------------------------------------*
REPORT z_dany_dyn_sort.
* s_field* - field name for sorting ( F1,F2,F3,F4,F5 or space )
* s_ord* - ASC or DES ( ascending or descending)
 
PARAMETERS: s_field1(6) DEFAULT 'FIELD1', s_ord1(3) DEFAULT 'ASC',
            s_field2(6) DEFAULT 'FIELD2', s_ord2(3) DEFAULT 'DES',
            s_field3(6) DEFAULT 'FIELD3', s_ord3(3) DEFAULT 'ASC',
            s_field4(6) DEFAULT 'FIELD4', s_ord4(3) DEFAULT 'DES',
            s_field5(6) DEFAULT 'FIELD5', s_ord5(3) DEFAULT 'ASC'.
 
TYPES: BEGIN OF ltt_fields,
       field1(6),
       field2(6),
       field3(6),
       field4(6),
       field5(6),
END OF ltt_fields.
 
DATA: l_field_asc1(6),
      l_field_asc2(6),
      l_field_asc3(6),
      l_field_asc4(6),
      l_field_asc5(6),
      l_field_des1(6),
      l_field_des2(6),
      l_field_des3(6),
      l_field_des4(6),
      l_field_des5(6),
      lt_fields TYPE TABLE OF ltt_fields,
      ls_fields TYPE ltt_fields,
      l_flag_invalid_field,
      l_flag_not_asc_des.
 
FIELD-SYMBOLS <fs> TYPE ANY.
 
INITIALIZATION.
 
* Just to fill an internal tables for testing
 DO 3 TIMES.
  ls_fields-field1 = sy-index.
  DO 3 TIMES.
    ls_fields-field2 = sy-index.
    DO 3 TIMES.
      ls_fields-field3 = sy-index.
      DO 3 TIMES.
        ls_fields-field4 = sy-index.
        DO 3 TIMES.
          ls_fields-field5 = sy-index.
          APPEND ls_fields TO lt_fields.
        ENDDO.
      ENDDO.
    ENDDO.
  ENDDO.
ENDDO.
 
START-OF-SELECTION.
 
* The order must be "ASC" or "DES" or space, any other value is rejected
l_flag_not_asc_des = 'X'.
CHECK ( s_ord1 = 'ASC' OR s_ord1 = 'DES' OR s_ord1 IS INITIAL ) AND
      ( s_ord2 = 'ASC' OR s_ord2 = 'DES' OR s_ord2 IS INITIAL ) AND
      ( s_ord3 = 'ASC' OR s_ord3 = 'DES' OR s_ord3 IS INITIAL ) AND
      ( s_ord4 = 'ASC' OR s_ord4 = 'DES' OR s_ord4 IS INITIAL ) AND
      ( s_ord5 = 'ASC' OR s_ord5 = 'DES' OR s_ord5 IS INITIAL ).
CLEAR l_flag_not_asc_des.
 
* the field name must be = "FIELD1, 2, 3, 4 or 5", any other value is rejected
l_flag_invalid_field = 'X'.
CHECK 'FIELD1FIELD2FIELD3FIELD4FIELD5' CS s_field1 AND
      'FIELD1FIELD2FIELD3FIELD4FIELD5' CS s_field2 AND
      'FIELD1FIELD2FIELD3FIELD4FIELD5' CS s_field3 AND
      'FIELD1FIELD2FIELD3FIELD4FIELD5' CS s_field4 AND
      'FIELD1FIELD2FIELD3FIELD4FIELD5' CS s_field5.
CLEAR l_flag_invalid_field.
 
 
* for a certain field, if the user ask descending order, the name of this field is
* moved in l_field_des1 AND it's important that l_field_asc1 remain empty.
IF s_field1 IS NOT INITIAL.
  IF s_ord1 = 'ASC'.
     l_field_asc1 = s_field1.
  ELSE.
     l_field_des1 = s_field1.
  ENDIF.
ENDIF.
 
IF s_field2 IS NOT INITIAL.
  IF s_ord2 = 'ASC'.
    l_field_asc2 = s_field2.
  ELSE.
    l_field_des2 = s_field2.
  ENDIF.
ENDIF.
 
IF s_field3 IS NOT INITIAL.
  IF s_ord3 = 'ASC'.
    l_field_asc3 = s_field3.
  ELSE.
    l_field_des3 = s_field3.
  ENDIF.
ENDIF.
 
IF s_field4 IS NOT INITIAL.
  IF s_ord4 = 'ASC'.
    l_field_asc4 = s_field4.
  ELSE.
    l_field_des4 = s_field4.
  ENDIF.
ENDIF.
 
IF s_field5 IS NOT INITIAL.
  IF s_ord5 = 'ASC'.
    l_field_asc5 = s_field5.
  ELSE.
    l_field_des5 = s_field5.
  ENDIF.
ENDIF.
 
* EACH field is used twice in the sort with different name for ascending and descending. 1 of the
* 2 fields will be empty and the sort will ignore it.
SORT lt_fields BY (l_field_asc1) ASCENDING (l_field_des1) DESCENDING
                  (l_field_asc2) ASCENDING (l_field_des2) DESCENDING
                  (l_field_asc3) ASCENDING (l_field_des3) DESCENDING
                  (l_field_asc4) ASCENDING (l_field_des4) DESCENDING
                  (l_field_asc5) ASCENDING (l_field_des5) DESCENDING.
 
* Display the results
EDITOR-CALL FOR lt_fields.
 
 
END-OF-SELECTION.
* if parameters was not entered correctly
 IF l_flag_not_asc_des = 'X'.
   WRITE: / 'Only ASC for ascending or DES for DESCENDING are allowed for fields S_ORDn'.
 ELSEIF l_flag_invalid_field = 'X'.
   WRITE: / 'S_FIELDn must be = FIELD1, FIELD2, FIELD3, FIELD4 or FIELD5.'.
ENDIF.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.