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

Sort a dynamic internal table



 
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: Wed Jul 30, 2008 3:53 pm    Post subject: Sort a dynamic internal table Reply with quote

put your fields in an i_tab then use
sort tablename by ( i_tab ).[i_tab is your internal table]

Code:
PARAMETERS dbtab TYPE c LENGTH 30.

SELECT-OPTIONS columns FOR dbtab NO INTERVALS.

DATA: otab TYPE abap_sortorder_tab,
oline TYPE abap_sortorder,
dref TYPE REF TO data.

FIELD-SYMBOLS: <column> LIKE LINE OF columns,
<itab> TYPE STANDARD TABLE.

TRY.
CREATE DATA dref TYPE STANDARD TABLE OF (dbtab).

****I have assigned the value dref to internal table as follows****

ASSIGN dref->* TO <itab>.
CATCH cx_sy_create_data_error.
MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.

TRY.
SELECT *
FROM (dbtab)
INTO TABLE <itab>.
CATCH cx_sy_dynamic_osql_semantics.
MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.

LOOP AT columns ASSIGNING <column>.
oline-name = <column>-low.
APPEND oline TO otab.
ENDLOOP.

TRY.
SORT <itab> BY (otab).
CATCH cx_sy_dyn_table_ill_comp_val.
MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
LEAVE PROGRAM.
ENDTRY.


Example 2
Sort mechanism respecting field type

It is often not enough to sort an internal table by text or binary value
but respecting the field type, e.g. a date field. In this case you have to
create a sort table with the sort fields as seperate rows. This small
example illustrates this:

Code:
date otab TYPE abap_sortorder_tab.
date oline TYPE abap_sortorder.

oline-descending = 'X'.
oline-name = 'CREATE_DATE'. APPEND oline to otab.
oline-name = 'CREATE_TIME'. APPEND oline to otab.
oline-name = 'CUSTOMER'. APPEND oline to otab.

SORT table_to_sort BY (otab).



Example 3

Armann wrote:
объявите str_order обычной внутренней таблицей, должно помочь

например:
Code:

data: str_order(72) type c occurs 2 with header line.

str_order = 'ebeln'. append str_order.
str_order = 'ebelp'. append str_order.

SORT xtable BY (str_order).


Example 4 by qhost

Code:
report ztest.

data: begin of tab1 occurs 0,
        col1(4)  type c,
        col2(10) type c,
      end of tab1.

start-of-selection.
  tab1-col1 = 'C'.
  append tab1.
  tab1-col1 = 'B'.
  append tab1.
  tab1-col1 = 'A'.
  append tab1.
  data: max(4) type c.
  perform get_max using 'TAB1'
                      'COL1'
                changing max.

form get_max using u_tab_name
                 u_field_name
           changing c_value.

  data: l_tab_name(132) type c,
        lref type ref to data,
        lt_sort_tab  type abap_sortorder_tab,
        ls_sort_line type abap_sortorder.

  field-symbols: <lfs_t_tab> type standard table,
                 <lfs_s_struc>,
                 <lfs_field>.

  concatenate u_tab_name '[]' into l_tab_name.

  assign (l_tab_name) to <lfs_t_tab>.
  check <lfs_t_tab> is assigned.

  try.
      create data lref
        like line of <lfs_t_tab>.
      assign lref->* to <lfs_s_struc>.
    catch cx_sy_create_data_error.
      message 'Create data error ! ' type 'E'.
  endtry.

  ls_sort_line-name = u_field_name.
  append ls_sort_line to lt_sort_tab.

  try.
      sort <lfs_t_tab> by (lt_sort_tab).
    catch cx_sy_dyn_table_ill_comp_val.
      message 'Wrong field' type 'E'.
  endtry.

  read table <lfs_t_tab>
    assigning <lfs_s_struc>
    index 1.
  if sy-subrc eq 0.
    assign component u_field_name
      of structure <lfs_s_struc> to <lfs_field>.
    check <lfs_field> is assigned.

    c_value = <lfs_field>.
  endif.
endform.
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.