2007 Jul 27 3:07 PM
Hi,
I want to sort the data in the following manner....
C 800
C 600
A 300
A 200
A 50
B 150
B 75
and I'm having the data like....
A 300
A 200
A 50
B 150
B 75
C 800
C 600
i.e second column descending and among the unique materials...
Is there any way.??
Regards
Jiku
2007 Jul 27 3:12 PM
oh...sorry..deleted
Message was edited by:
Chandrasekhar Jagarlamudi
2007 Jul 27 3:21 PM
Fld1 Fld2 Fld3
A 300
A 200
A 50
B 150
B 75
C 800
C 600
Fld1 and fld2 are the fields of itab.
Sorting is to be done: FLD1- Descending, FLD2- Descending
<b>SORT itab DESCENDING by FLD1 FLD2.</b>
or <i>If you have itab which is to sorted by descending by some fields and ascending some fields... then follow this procedure..</i>
declare an internal table OTAB of type ABAP_SORTORDER_TAB.
append two records :
NAME : FLD2
DESCENDING : 'X'
ASTEXT
NAME : FLD1
DESCENDING : 'X' " If Descending use ''
ASTEXT
now do
SORT itab by (otab).
For more info :
<i>
With the addition BY (otab), the table is not sorted according to the table key, but instead according to the component specified dynamically in the internal table otab (as of release 7.0). Each line of the table otab defined a component of the sort key. The priority of the sort is based on the order of the lines in otab. A maximum of 250 components can be specified. If the table otab is initial, the table is not sorted.
For otab, a standard table of the table type ABAP_SORTORDER_TAB from the ABAP Dictionary must be specified. The line type of this table is the Dictionary structure ABAP_SORTORDER with the following components:
NAME of type SSTRING
for specifying a component of the sort key. The component is entered in the form "comp_name[off(len)]", whereby "comp_name" must be the name of a component contained in itab in upper case to which an offset/length specification "off(len)" can be appended. "off" and "len" must be suitable numeric values.
DESCENDING of type CHAR of length 1 for specifying the
sort direction for the current component. If DESCENDING is initial, the sort is performed in ascending order. If DESCENDING has the value "X", the table is sorted in descending order.
ASTEXT of type CHAR of length 1
for textual sorting of the current component. If ASTEXT has the value "X", the sort is performed as with the AS TEXT addition. This is only possible for character-type components. If ASTEXT is initial, character-type components are sorted according to their binary representation.
If a column of otab, has invalid content (NAME contains the name of a component that does not exist or an incorrect offset/length specification, DESCENDING and ASTEXT do not contain "X" or the initial value), this leads to a treatable exception of the class CX_SY_DYN_TABLE_ILL_COMP_VAL.
<b>Notes: </b>
The addition BY (otab) cannot be combined with BY compi.
When using the addition BY (otab), it is not possible to use DESCENDING or AS TEXT to specify a descending sort direction or textual sorting for all components.
If a single bracketed data object (dobj) is specified after the BY addition, its data type decides whether its content is used to specify a single component or multiple components. In either case, no sort takes place if dobj is initial.
Example:
Dynamic import of a database table into a dynamic internal table and dynamic sorting of its content. The name of the database table and the names of the columns by which the table is to be sorted can be entered on a selection screen.
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).
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.
</i>
Reward if useful
Regards
Prax
Message was edited by:
Prax
2007 Jul 27 3:30 PM
Hi,
Please create new internal table ITAB2 same as ITAB1 and add new field SORT.
loop at itab.
case field1.
when 'C'.
itab2-sort = '1'.
when 'A'.
itab2-sort = '2'.
when 'B'.
itab2-sort = '3'.
endcase.
itab2-field1 = itab1-field1.
itab2-field2 = itab1-field2.
append itab2.
endloop.
sort itab2 by sort ascending field2 descending.
loop at itab2.
write: / itab2-field1, itab2-field2.
endloop.
Regards,
Ferry Lianto
2007 Jul 27 3:36 PM
Use the following command .
sort itab by field1 descending as text
field 2 decending .
reward points if usefull.
Thanks and regards,
Veerendranath M