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: 

how to use sort using abap objects

Former Member
0 Kudos

hello friends,

i am using the class cl_salv_table to display my ALV. now on my selection screen i have 3 radio buttons,according to the radiobutton selected my output should appear sorted.

which class can help me sort my internal fields according to the radio button on the selection screen and pls tell me how to do this.

1 REPLY 1

marcelo_ramos
Active Contributor
0 Kudos

Hi Amit,

You must use the methods GET_SORTS and ADD_SORT of class CL_SALV_SORTS.

You can get more explanation on <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/5dc3e690-0201-0010-1ebf-b85b3bed962d">SAP List Viewer Document</a>.

Here is an example, i hope it useful for you.


REPORT zsalv_sort_mar NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
  PUBLIC SECTION.

    METHODS select_alv IMPORTING amount TYPE i.

    METHODS display_alv IMPORTING columnname TYPE lvc_fname.

  PROTECTED SECTION.

    DATA: o_table   TYPE REF TO cl_salv_table,
          " We Must create an object of cl_salv_sorts to sort table
          o_sorts   TYPE REF TO cl_salv_sorts,
          o_columns TYPE REF TO cl_salv_columns_table.

    DATA t_alv TYPE STANDARD TABLE OF alv_tab.

ENDCLASS.                    "lcl_alv DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_alv IMPLEMENTATION.

  METHOD select_alv.

    SELECT *
      FROM alv_tab
      INTO CORRESPONDING FIELDS OF TABLE t_alv
          UP TO amount ROWS.

  ENDMETHOD.                    "SELECT_ALV

  METHOD display_alv.

    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table = o_table
          CHANGING
            t_table      = t_alv ).
      CATCH cx_salv_msg.
    ENDTRY.

    o_columns = o_table->get_columns( ).
    o_columns->set_optimize( abap_true ).

    "Here we're getting all sortable field
    o_sorts = o_table->get_sorts( ).
    "COLUMNNAME contais 'CARRNAME' that will be used to the sort Table  
    o_sorts->add_sort( COLUMNNAME ).
    o_table->display( ).

  ENDMETHOD.                    "display_alv

ENDCLASS.                    "lcl_alv IMPLEMENTATION

DATA o_alv TYPE REF TO lcl_alv.

START-OF-SELECTION.

  CREATE OBJECT o_alv.

  o_alv->select_alv( 30 ).
 "Displaye alv Sorting by field 'CARRNAME' that will be passed to
 "method add_sort of class cl_salv_sorts
  o_alv->display_alv( 'CARRNAME' ).

Regards.

Marcelo Ramos