‎2008 May 07 8:54 AM
Hi,
Has anyone implemented a sorting of Hexadecimal structure data based on any order of key or non-key fields?
I have an internal table ITAB1, of structure of a table T1 (determined at run-time, no way of knowing the structure beforehand). I transfer valyes of ITAB1 to ITAB2 which is of Hexadecimal type of structure T1. Now, I use ITAB2 on list display.
How do I sort the entries of ITAB2 based on any field of T1, determined at run-time? Any ideas?
Any helpful answer will be greatly appreciated, points assured!
Regards,
Rekha
‎2008 May 07 8:56 AM
Hai,
If a sort criterion is not known until runtime, you can use SORT itab BY ... (name) ... to specify it dynamically as the contents of the field name . If name is blank at runtime, the sort criterion is ignored. If name contains an invalid component name, a runtime error occurs.
You can use offset and length specifications to further restrict sort criteria, regardless of whether they are specified statically or dynamically.
If itab is an internal table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. If the field symbol does not point to the header line of the internal table, a runtime error occurs.
Note
Performance
The runtime required to sort an internal table increases with the number of entries and the width of the sort key.
Sorting an internal table with 100 entries and the 50-byte wide default key would take about 1300 msn (standardized microseconds). A 30-byte wide sort key would need about 950 msn.
If one of the specified sort criteria is itself an internal table, the SORT may sometimes take longer.
Runtime errors
SORT_ITAB_FIELD_INVALID :A field symbol used as a dynamic sort criterion does not point to the header line of the internal table to be sorted.
SORT_TOO_MANY_FIELDS : More than 250 sort criteria.
Related APPEND ... SORTED BY
Variant 2
SORT.
Additions
1. ... DESCENDING (similar to variant 1)
2. ... ASCENDING (similar to variant 1)
3. ... BY f1 f2 ... fi
4. ... BY fg
Effect
Sorts the dataset generated with EXTRACT by the fields in the field group HEADER (see FIELD-GROUPS ).
Here, blank fields (i.e. fields not defined with EXTRACT ) are inserted before all non-blank fields, regardless of whether the sort sequence is in ascending or descending order.
Notes
The number of sort criteria is restricted to 50.
As with variant 1, any sequence of fields you specify for sorting purposes does not remain fixed. Any sequence of records which belongs to different field groups, but has the same HEADER field contents, is arbitrary.
Again like variant 1, sorting takes place in main memory if at all possible. If there is insufficient space there, ABAP/4 calls an external sort program. You can modify the directory used to create the temporary auxiliary file by modifying the SAP profile parameter DIR_SORTTMP .
As soon as a dataset has been processed with SORT or LOOP ... ENDLOOP , you cannot extract any more records with EXTRACT .
Addition 3
... BY f1 f2 ... fi
Effect
Can sort only by fields in the field group HEADER .
Otherwise, the effect is similar to variant 1.
Addition 4
... BY fg
Effect
Sorts by the fields in field group fg .
However, the only fields which can be sorted are those in the field group HEADER , i.e. the field group fg can consist only of fields from the field group HEADER (see INSERT ... INTO ).
Example
DATA: ONR(7), DATE TYPE D, POSITION(3) TYPE N,
CUSTOMER(16),
PNR(5) TYPE N, NAME(10), UNITS TYPE I,
ORDERS TYPE I.
FIELD-GROUPS: HEADER, ORDER, PRODUCT, DATE_FIRST.
INSERT ONR DATE POSITION INTO HEADER.
INSERT CUSTOMER INTO ORDER.
INSERT PNR NAME UNITS INTO PRODUCT.
INSERT DATE ONR POSITION INTO DATE_FIRST.
ONR = 'GF00012'. DATE = '19921224'.
POSITION = '000'. CUSTOMER = 'Good friend (2.)'.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = '12345'. NAME = 'Screw'. UNITS = 100.
EXTRACT PRODUCT.
ADD 1 TO POSITION.
PNR = '23456'. NAME = 'Nail'. UNITS = 200.
EXTRACT PRODUCT.
ONR = 'MM00034'. DATE = '19920401'.
POSITION = '000'. CUSTOMER = 'Moneymaker'.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = '23456'. NAME = 'Nail'. UNITS = 300.
EXTRACT PRODUCT.
ADD 1 TO POSITION.
PNR = '34567'. NAME = 'Hammer'. UNITS = 4.
EXTRACT PRODUCT.
ONR = 'GF00011'. DATE = '19921224'.
POSITION = '000'. CUSTOMER = 'Good friend (1.)'.
EXTRACT ORDER.
ADD 1 TO POSITION.
PNR = '34567'. NAME = 'Hammer'. UNITS = 5.
EXTRACT PRODUCT.
SORT BY DATE_FIRST.
LOOP.
AT ORDER.
WRITE: /, / DATE, ONR, POSITION,
CUSTOMER, 'ordered:'.
ENDAT.
AT PRODUCT.
WRITE: / DATE, ONR, POSITION,
PNR, NAME, UNITS.
ENDAT.
ENDLOOP.
This generates the following output:
01041992 MM00034 000 Moneymaker ordered:
01041992 MM00034 001 23456 Nail 300
01041992 MM00034 002 34567 Hammer 4
24121992 GF00011 000 Good friend (1.) ordered:
24121992 GF00011 001 34567 Hammer 5
24121992 GF00012 000 Good friend (2.) ordered:
24121992 GF00012 001 12345 Screw 100
24121992 GF00012 002 23456 Nail 200
Note
Performance
The runtime required to sort an internal table increases with the number of entries and the width of the sort key.
Note
Runtime errors
SORT_EXTRACT_TOO_MANY_FIELDS : More than 50 sort criteria
SORT_FIELD_NOT_IN_HEADER : Sort criterion not in field group HEADER
SORT_NO_HEADER : Field group HEADER not created
SORT_WITHIN_LOOP : SORT on extract dataset within LOOP on extract dataset
Thanks,
Swetha Singh
‎2008 May 07 9:01 AM
Hi Swetha,
The ITAB2 that I mention is a hexadecimal table, which contains a single value with hexadecimal content. This content is mapped to the table structure via field-symbol assignments. ITAB2 doesnt know a header line, as the table is a Field-Symbol itself.
It doesn't recognise a header structure at all, and hence grouping fields and passing to SORT BY criteria doesn't work..
Any other ideas?
Cheers
Rekha
Edited by: Rekha S on May 7, 2008 1:33 PM