Skip to Content
Author's profile photo Former Member

To Sort Table Maintenance Before Display.

I found the below steps very useful to sort the table maintenance before display.

  • Go to SE11 and enter table name(eg. ZTEST) ->Display
  • Click UTILITIES menu->Table Maintenance Generator
  • Click on ENVIRONMENT menu->Modification->Maintenance screens->Double click on record in the popup.
  • Write one MODULE SORT_EXTRACT (in PBO of the maintenance view screen) to sort exactly before Table Control loop statement(LOOP AT EXTRACT WITH CONTROL)
  • Write the below piece of code at SORT_EXTRACT to sort Table Maintenance.

    MODULE SORT_EXTRACT OUTPUT.

    DATA : IT_ZTEST LIKE ZTEST OCCURS 1WITHHEADER LINE .

    IF NOT SY-UCOMM = ‘NEWL’.

    IT_ZTEST[] = EXTRACT[].

    SORT IT_ZTEST BY F1 F2 F3.

    EXTRACT[] = IT_ZTEST[].

    ENDIF.

    ENDMODULE” SORT_EXTRACT OUTPUT


    Source: An SAP Consultant: SAP ABAP – sorting Table maintenance

Assigned Tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jörg Sauterleute
      Jörg Sauterleute

      I have "optimized" the MODULE Content, to be able to use it more than once

      CODE:

      CASE objh-objectname.    "global field 🙂

        WHEN '<YOUR_MAINTVIEW>'. 

          DATA : IT_ZTEST LIKE <YOUR_MAINTVIEW> OCCURS 1 WITH HEADER LINE .

           [...]

      ENDCASE.

      Author's profile photo Jefferson Misiak
      Jefferson Misiak

      Hi Chitra Sankar,

      Very nice, thanks.

       

      Author's profile photo Leanid Tuzik
      Leanid Tuzik

      Hello Chitra, it doesn't work, I've a dump  OBJECTS_TABLES_NOT_COMPATIBLE ( description: Two internal tables are not compatible or convertible )

      Author's profile photo Manoj Mohanty
      Manoj Mohanty

      Hi Chitra / Leanid,

      I have modified above code for better performance.

        DATA lt_tab_agent TYPE STANDARD TABLE OF <YOUR TABLE NAME>.
      
        FIELD-SYMBOLS: <lfs_xfrom> TYPE x, "Hexadecimal value of from value
                       <lfs_xto>   TYPE x. "Hexadecimal value of to value
      
        IF NOT sy-ucomm = 'NEWL'.
      
          CLEAR lt_tab_agent[].
      
          LOOP AT extract.
      
            APPEND INITIAL LINE TO lt_tab_agent ASSIGNING <lfs_xto> CASTING.
            ASSIGN extract TO <lfs_xfrom> CASTING.
      
            <lfs_xto> = <lfs_xfrom>.
      
          ENDLOOP.
      
          SORT lt_tab_agent BY <REQUIRED FIELDS>.
      
          REFRESH extract.
          LOOP AT lt_tab_agent INTO <YOUR TABLE NAME>.
      
            APPEND INITIAL LINE TO extract ASSIGNING <lfs_xto> CASTING.
            ASSIGN <YOUR TABLE NAME> TO <lfs_xfrom> CASTING.
      
            <lfs_xto> = <lfs_xfrom>.
      
          ENDLOOP.
      
        ENDIF.

       

       

      Author's profile photo Ankit Maskara
      Ankit Maskara

      Thanks, quick and elegant.

      Author's profile photo Former Member
      Former Member

      Hi,Mr.Manoj Mohanty, thank you. this is a alternative solution to my question herehttps://answers.sap.com/questions/12903934/how-to-handle-this-error-assign-base-wrong-alignme.html?childToView=12903953&answerPublished=true#answer-12903953

      Can you help check why that is happenning?

      Author's profile photo Shubham Patil
      Shubham Patil

      boss ! thanks this is working, but when we manipulate EXTRACT[] or TOTAL[] then standard

      functionalities like SAVE, DELETE go high and they do not work in fact the memory id

      (SAPLZMM_E006)<ACTION> is set to '#' instead of 'U' , so can  u help on this ?

      Author's profile photo Helen Silva
      Helen Silva

      Hi Manoj Mohanty,

      it works fine for sorting, but I can't delete rows. I have the message:

      "Select entries before performing the function".

       

      Author's profile photo Manoj Mohanty
      Manoj Mohanty

      Hi Helen,

      Try like this.

        DATA: lt_tab_agent TYPE STANDARD TABLE OF zbc_mail_support,
              lrt_ucomm    TYPE RANGE OF sy-ucomm INITIAL SIZE 0,
              lwa_ucomm    LIKE LINE OF lrt_ucomm.
      
        FIELD-SYMBOLS: <lfs_xfrom> TYPE x, "Hexadecimal value of from value
                       <lfs_xto>   TYPE x. "Hexadecimal value of to value
      
        REFRESH lrt_ucomm.
        CLEAR lrt_ucomm[].
      
      * Prepare exclude list range for user command
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'NEWL'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'DELE'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'KOPE'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'ORGI'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'MKAL'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'MKBL'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        lwa_ucomm-sign = 'I'.   lwa_ucomm-option = 'EQ'.
        lwa_ucomm-low = 'MKLO'. lwa_ucomm-high = ''.
        APPEND lwa_ucomm TO lrt_ucomm. CLEAR lwa_ucomm.
      
        IF NOT sy-ucomm IN lrt_ucomm.
      
        <Write your logic Here>
      
        ENDIF.

       

      Thanks,

      Manoj

       

      Author's profile photo Christian Hannemann
      Christian Hannemann

      If you want to be indepentent from regeneration, you can try event 10 with the following coding:

      form sort.
      
        data: lt_data type standard table of zdata.
      
        field-symbols: <ls_data> like line of lt_data.
      
        check sy-ucomm <> 'SAVE'.
      
        loop at total[] assigning <ls_data> casting.
          append <ls_data> to lt_data.
        endloop.
      
        sort lt_data by field.
      
        clear: total[], total.
      
        loop at lt_data into <vim_total_struc>.
          append total to total[].
        endloop.
      
      endform.

       

      Best regards,

      Christian

      Author's profile photo Kyle McCarter
      Kyle McCarter

      Thank you Christian. Simple and straight forward.

      Author's profile photo Martin Gill
      Martin Gill

      Hey, I'm a Functional Consultant so hopefully my code is not too woeful.  I created a cluster view using transaction SE54.  The records were not sorted, so I created a variation on the code above but tried to keep it generic so I can use the same module for any table to sort the underlying view by the primary index (a.k.a. key).

      *&      Module  SORT_EXTRACT_BY_KEY  OUTPUT
      *&---------------------------------------------------------------------*
      *       Sort tables by the primary index when drilling down from SM34  *
      *       view created using SE54                                        *
      *----------------------------------------------------------------------*
      MODULE sort_extract_by_key OUTPUT.
        TYPES:
          BEGIN OF lty_sortfield,
            name     TYPE fieldname,
            flg_desc TYPE char1,
          END OF lty_sortfield.
        DATA:
          lt_dd03l     TYPE STANDARD TABLE OF dd03l"Data dictionary table fields
          ls_dd03l     LIKE LINE OF lt_dd03l,
          lt_sortfield TYPE STANDARD TABLE OF lty_sortfield"Sort fields
          ls_sortfield LIKE LINE OF lt_sortfield,
          dref_struct  TYPE REF TO data,
          dref_table   TYPE REF TO data.
        FIELD-SYMBOLS:
          <fs_xfrom> TYPE x,              "Hexadecimal value of from value
          <fs_xto>   TYPE x,              "Hexadecimal value of to value
          <fs_struc> TYPE any,            "View being processed
          <fs_table> TYPE STANDARD TABLE"View table
      * Do not update when creating new lines
        IF NOT sy-ucomm 'NEWL'.
      * The view name must be available & there must be key fields
      * The field worked in my testing but may not in every scenario
          CHECKview_name IS NOT INITIAL. 
      * Dynamically assign view to field symbol
          CREATE DATA dref_struct TYPE (view_name).
          ASSIGN dref_struct->TO <fs_struc>.
          CREATE DATA dref_table TYPE STANDARD TABLE OF (view_name).
          ASSIGN dref_table->TO <fs_table>.
      * Transfer hexadecimal extract to view table
          APPEND INITIAL LINE TO <fs_table> ASSIGNING <fs_struc>.
          REFRESH<fs_table>.
          LOOP AT extract.
            APPEND INITIAL LINE TO <fs_table> ASSIGNING <fs_xto> CASTING.
            ASSIGN extract TO <fs_xfrom> CASTING.
            <fs_xto> <fs_xfrom>.
          ENDLOOP.
      * Sort the table by the primary index
          REFRESHlt_dd03llt_sortfield.
      * Firstly, get the primary index fields
          SELECT fieldname position
            FROM dd03l
            INTO CORRESPONDING FIELDS OF TABLE lt_dd03l
            WHERE tabname    view_name
              AND keyflag    'X'.
      *       AND fieldname NE 'MANDT'. "No issues in testing if MANDT included
          SORT lt_dd03l BY position.
      * Build an internal table containing the fields
          LOOP AT lt_dd03l INTO ls_dd03l.
            ls_sortfield-name     ls_dd03l-fieldname.
            ls_sortfield-flg_desc space.
            APPEND ls_sortfield TO lt_sortfield.
          ENDLOOP.
      
      * Sort the internal table (type ANY so can't specify the fields)
          CALL FUNCTION 'C140_TABLE_DYNAMIC_SORT'
            EXPORTING
              i_flg_not_case_sensitive 'X'
            TABLES
              i_sortfield_tab          lt_sortfield
              x_tab                    <fs_table>
            EXCEPTIONS
              sortfieldtab_too_big     1
              OTHERS                   2.
      * Transfer the sorted values back to the original tab in hexadecimal
          REFRESH extract[].
          LOOP AT <fs_table> ASSIGNING <fs_struc>.
            APPEND INITIAL LINE TO extract ASSIGNING <fs_xto> CASTING.
            ASSIGN <fs_struc> TO <fs_xfrom> CASTING.
            <fs_xto> <fs_xfrom>.
          ENDLOOP.
        ENDIF.
      ENDMODULE.
      

       

      Author's profile photo Philipp Bodenmann
      Philipp Bodenmann

      I was facing the same problem. I was able to sort but couldn't delete or copy rows.

      The following solution fit for me to sort and still being able to delete and copy correctly:

      MODULE sort_extract OUTPUT.
      
        SORT extract BY table_line+5(8) DESCENDING
                        table_line+3(1) ASCENDING. 
      
      ENDMODULE.