abap

sorting some column of an internal table that meet a condition


I have an internal table

standard internal table

and i want to display it like this:

like this.

note that only a1's date is sorted descending. so im wondering if theres a method to only sort dates belong to a1 while leaving the others untouched.

I am a beginner so id appreciate if the answer is easy to understand


Solution

  • You can use the LOOP/GROUP approach to sort the table before sending it to the ALV (sample code only):

        LOOP AT itab ASSIGNING FIELD-SYMBOL(<line>)
                         GROUP BY ( key1 = <line>-cust_id key2 = <line>-prod_id ) ASCENDINGdd
                         ASSIGNING FIELD-SYMBOL(<grp1>).
    
              LOOP AT GROUP <grp1> ASSIGNING FIELD-SYMBOL(<member>).
                members = VALUE #( BASE members ( <member> ) ).
              ENDLOOP.
    
              IF <grp1>-prod_id = 'A1'.
                SORT members BY sale_date DESCENDING.
              ELSE.
                SORT members BY sale_date ASCENDING.
              ENDIF.
    
            APPEND LINES OF members TO it_final.
        ENDLOOP.
    

    Reference: https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abaploop_at_itab_group_by.htm