2006 Aug 23 2:50 PM
I have an internal table lt_refs of the type ZXX_TT_REFS.
The table type ZXX_TT_REFS is a table of references ("ref. type") to the class ZCL_C.
The class C has an attribute attr1.
Now I would like to sort that table. Is there an easy (built-in) way to do this?
DATA lt_refs TYPE ZXX_TT_REFS.
DATA lr_ref TYPE REF TO ZCL_C.
LOOP AT lt_refs INTO lr_ref.
"Sort based on lr_ref->attr1. ?
ENDLOOP.
"or can I
SORT lt_refs BY attr1.
"directly?
2006 Aug 23 3:01 PM
You can sort by referencing the attribute of the object. See below.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_tab DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_tab definition.
public section.
types: ttab type table of string.
data: itab type ttab.
methods: constructor importing im_tab type ttab.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_tab IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_tab implementation.
method constructor.
itab[] = im_tab[].
endmethod.
endclass.
data: l_itab type table of string.
data: l_wa type string.
data: a_tab type ref to lcl_tab.
start-of-selection.
l_wa = 'B'. append l_wa to l_itab.
l_wa = 'C'. append l_wa to l_itab.
l_wa = 'D'. append l_wa to l_itab.
l_wa = 'A'. append l_wa to l_itab.
create object a_tab
exporting
im_tab = l_itab.
<b> sort a_tab->itab ascending.</b>
check sy-subrc = 0.
REgards,
RIch Heilman
2006 Aug 23 3:01 PM
You can sort by referencing the attribute of the object. See below.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_tab DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_tab definition.
public section.
types: ttab type table of string.
data: itab type ttab.
methods: constructor importing im_tab type ttab.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_tab IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_tab implementation.
method constructor.
itab[] = im_tab[].
endmethod.
endclass.
data: l_itab type table of string.
data: l_wa type string.
data: a_tab type ref to lcl_tab.
start-of-selection.
l_wa = 'B'. append l_wa to l_itab.
l_wa = 'C'. append l_wa to l_itab.
l_wa = 'D'. append l_wa to l_itab.
l_wa = 'A'. append l_wa to l_itab.
create object a_tab
exporting
im_tab = l_itab.
<b> sort a_tab->itab ascending.</b>
check sy-subrc = 0.
REgards,
RIch Heilman
2006 Aug 23 3:56 PM
Hi Rich,
sure, I can sort an internal table that is the attribute of a class.
By how can I sort an interal table that itself consists of a list of references to objects of a class?
2006 Aug 23 4:03 PM
2006 Aug 23 4:17 PM
And for what purpose? They are simply dynamically created references.
2006 Aug 23 4:26 PM
Danial, please see the following. In the case where you want to sort your reference by an attribute within the object, you can do something like this.
report zrich_0001.
*---------------------------------------------------------------------*
* CLASS lcl_tab DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app definition.
public section.
data: attri type i.
methods: constructor importing im_attri type i.
endclass.
*---------------------------------------------------------------------*
* CLASS lcl_tab IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
class lcl_app implementation.
method constructor.
attri = im_attri.
endmethod.
endclass.
data: a_app type ref to lcl_app.
data: a_app_list type table of ref to lcl_app.
start-of-selection.
create object a_app exporting im_attri = 3 .
append a_app to a_app_list.
create object a_app exporting im_attri = 2 .
append a_app to a_app_list.
create object a_app exporting im_attri = 1 .
append a_app to a_app_list.
sort a_app_list by <b>table_line->attri</b> ascending .
check sy-subrc = 0.
Here is the documentation.
<i>
<b>
Access to Attributes with References in Internal Tables</b>
If the line type of internal tables includes reference variables as components comp, the attributes attr of the object to which the reference in a line points can be used as key values for reading, sorting and changing table rows. This is possible in the following statements:
,,LOOP AT itab ... WHERE comp->attr ...
,,READ TABLE itab ... WITH [TABLE] KEY comp->attr = ...
<b>,,SORT itab BY comp->attr ...</b>
,,DELETE itab WHERE comp->attr ...
,,MODIFY itab ... TRANSPORTING ... WHERE comp->attr ...
<b>If a table contains unstructured lines with the type of a reference variable, the attributes of the object to which a line points can be addressed using TABLE_LINE->attr.</b>
</i>
Regards,
RIch Heilman
2006 Aug 24 8:02 AM
Tanks, Rich.
That's what I wanted.
I has something like this in mind, but I just couldn't find it in the doku.
Really cool!