Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

sorting

Former Member
0 Kudos

Hello

I was wondering if I could implement sorting in a classical list.

Let's take the following example:

.

.

.

select * from mara into corresponding fields of table itab

where ...

loop at itab .

write: \ itab-1, itab-2, itab-3.

endloop.

.

.

.

Can I sort the resulting list by itab-1 ?

10 REPLIES 10

Former Member
0 Kudos

select * from mara into corresponding fields of table itab

where ...

***insert thsi line****

sort itab by 1.

loop at itab .

write: \ itab-1, itab-2, itab-3.

endloop.

Rgds,

Mano

0 Kudos

Not that kind of sorting...

I meant interactive sorting. Like sorting in code by itab-1 and after the report is displayed sorting by itab-2 or otab-3 and so on.

0 Kudos

Vinod_Chandran
Active Contributor
0 Kudos

Hi George,

You cannot sort the output in the classical list. But this is possible in ALV.

If you want the sort function in classical list then you have to have a field in the selection screen through which the user can select the sort field and in the program you can sort the internal table based on this field before the output.

Thanks

Vinod

Former Member
0 Kudos

Check this link, might help u

Former Member
0 Kudos

Hi, though SORT can concatenate the dynamic string like the WHERE, but it's a little difference. Every string between parentheses means a field name, example like following:

  • it is ok

str1 = 'field1'.

str1 = 'field2'.

sort itab by (str1) ascending (str2) descending.

  • it will failed

str1 = 'field1 ascending field2 descending'.

sort itab by str1.

You can achieve it in two way:

first, dynamically sort in the select statement, code like following:


types:begin of typ_sort,
        field  type  char30,
      end of typ_sort.
data: it_sort     type standard table of typ_sort.
data: lc_sort     like line of it_sort.

lc_sort = 'CARRID DESCENDING'.
append lc_sort to it_sort.
lc_sort = 'CONNID'.
append lc_sort to it_sort.

select *
  from sflight
  into table it_sflight
  order by (it_sort).

Second, you can sort internal table dynamically too, reference the code in the following:


*&---------------------------------------------------------------------*
*& 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.

Hope it will be helpful.

Thanks

By the way, vinod mentioned the ALV sort, I'm interesting in how SAP achieve the dynamical sort in ALV, let me have a research.

andreas_mann3
Active Contributor
0 Kudos

Hi,

use fm RKE_SORT_ELEMENTS

Andreas

Former Member
0 Kudos

Hi, everybody

Just researched in the ALV.

Actually, the sap solve dynamical sort issue in ALV using the second way I mention.

FM LVC_KKB_OUTTAB_SORT will be called in the ALV sort.

And in its code, you can see a long sort statement, which list scores of fields in it.

Fill the T_OUTTAB with origin itab, IT_SORT with sort fields, then we will get the new sorted table T_OUTTAB.

I think we can directly call LVC_KKB_OUTTAB_SORT or LVC_SORT_APPLY(LVC_SORT_APPLY envelop LVC_KKB_OUTTAB_SORT, and we are more familiar with its interface), if we don't want to write the code repeatly.

Hope it will be helpful.

Thanks a lot

0 Kudos

Of course you can sort a classical list interactively. You would just be re-writing the list each time. Each time you sort you must decrement the list index by 1, it is a SY- value.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi George,

in my opinion, this example is the easiest way do this. The command <b>GET CURSOR FIELD fieldname</b> get the name of the sort-field. This example need a GUI status with a sort-button and the ucode 'SRTD'.

Just click to the sort column in the list and then click the sort-button.

I have no MARA on my testdrive, so I took the SFLIGHT for this example.


REPORT  z_classic_sort_example.

DATA: wa_sflight TYPE sflight,
      it_sflight LIKE TABLE OF wa_sflight,
      fieldname  TYPE fieldname.

START-OF-SELECTION.

  set pf-status 'STANDARD_WITH_SORT'.
  SELECT * FROM sflight INTO TABLE it_sflight.
  PERFORM display_flights.

AT USER-COMMAND.
  CASE sy-ucomm.
    WHEN 'SRTD'.
      GET CURSOR FIELD fieldname.
      fieldname = fieldname+11.
      SORT it_sflight BY (fieldname).
      PERFORM display_flights.
      sy-lsind = sy-lsind - 1.
  ENDCASE.


FORM display_flights.
  LOOP AT it_sflight INTO wa_sflight.
    WRITE: /1 wa_sflight-carrid,
              wa_sflight-connid,
              wa_sflight-fldate,
              wa_sflight-price.
    CLEAR wa_sflight.
  ENDLOOP.
ENDFORM.                    "display_flights

Bye,

Stefan