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: 

sort in particular order

former_member5472
Active Contributor
0 Kudos

hi,

i have to sort a field as

POSTF001

FIYRF001

POSTF002

FIYRF002

POSTF003

FIYRF003

please revert

4 REPLIES 4

matt
Active Contributor
0 Kudos

Break the field into two portions - say FIRST TYPE C LENGTH 5 and LAST TYPE C LENGTH 3. Then you can sort by LAST ASCENDING and FIRST DESCENDING. Thus:

TYPES: BEGIN OF rec,
         first TYPE c LENGTH 5,
         last  TYPE c LENGTH 3,
       END OF rec.

DATA: t_tab TYPE STANDARD TABLE OF rec.

FIELD-SYMBOLS <wa> TYPE rec.

APPEND 'FIYRF002' TO t_tab.
APPEND 'POSTF003' TO t_tab.
APPEND 'FIYRF001' TO t_tab.
APPEND 'POSTF001' TO t_tab.
APPEND 'POSTF002' TO t_tab.
APPEND 'FIYRF003' TO t_tab.

SORT t_tab BY last first DESCENDING.

LOOP AT t_tab ASSIGNING <wa>.
  WRITE: / <wa>.
ENDLOOP.

matt

GrahamRobbo
Active Contributor
0 Kudos

Nice one Matt,

this also works


  TYPES: BEGIN OF rec,
    field TYPE c LENGTH 8,
         END OF rec.

  DATA: t_tab TYPE STANDARD TABLE OF rec.

  FIELD-SYMBOLS <wa> TYPE rec.

  APPEND 'FIYRF002' TO t_tab.
  APPEND 'POSTF003' TO t_tab.
  APPEND 'FIYRF001' TO t_tab.
  APPEND 'POSTF001' TO t_tab.
  APPEND 'POSTF002' TO t_tab.
  APPEND 'FIYRF003' TO t_tab.

  SORT t_tab BY field+5(3).

  LOOP AT t_tab ASSIGNING <wa>.
    WRITE: / <wa>.
  ENDLOOP.

Cheers

Graham Robbo

0 Kudos

thnaks for the help

Former Member
0 Kudos

Hi,

You can sort a standard or hashed table in a program. To sort a table by its key, use the statement

SORT <itab> ASCENDING AS TEXT STABLE.

The statement sorts the internal table <itab> in ascending order by its key. The statement always applies to the table itself, not to the header line. The sort order depends on the sequence of the standard key fields in the internal table. The default key is made up of the non-numeric fields of the table line in the order in which they occur.

You can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending.

The larger the sort key, the more time the system needs to sort the table. If the sort key contains an internal table, the sorting process may be slowed down considerably.

You cannot sort a sorted table using the SORT statement. The system always maintains these tables automatically by their sort order. If an internal table is statically recognizable as a sorted table, the SORT statement causes a syntax error. If the table is a generic sorted table, the SORT statement causes a runtime error if the sort key is not the same as an extract of the beginning of the table key, you sort in descending order, or use the AS TEXT addition. In other words, the SORT statement is only allowed for generic internal tables, if it does not violate the internal sort order.

The number of sort fields is restricted to 250.

The sorting process is not stable, i.e. if no sort is performed for a predefined sequence of fields, the sequence is not retained.

To delete all duplicate entries from a sorted internal table, you can specify DELETE ADJACENT DUPLICATES FROM itab after SORT .

The sort itself uses the Quicksort process where the key fields for all the data records are retrieved and placed in an area of main memory.

If there is not enough space in memory, the key fields are written to a temporary file and sorted by an external sort program. You can modify the directory which the SORT uses to store such auxiliary files by modifying the SAP profile parameter DIR_SORTTMP . Normally, auxiliary files are created in the SAP data directory (SAP profile parameter DIR_DATA ).

Reward if found helpfull,

Cheers,

Chaitanya.