Application Development and Automation 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: 
Read only

How to sort internal table without sort verb

Former Member
0 Likes
2,774

sort an internal table - of course, without using the sort verb.

Table has two fields:

Key1(4) TYPE C,

data(8) TYPE C.

sort an internal table - of course, without using the sort verb.

Table has two fields:

Key1(4) TYPE C,

data(8) TYPE C.

6 REPLIES 6
Read only

Former Member
0 Likes
1,579

Hi Megha,

Declare the internal table as sorted table. see the syntax. then the table is automatically sort when you enter the entry.

Thanks,

Sunil

Read only

0 Likes
1,579

please tell me how to declare table as sorted table

Read only

Former Member
0 Likes
1,579

u must hav declared table as

data: it_tab type standard table of <>.

instead of dat use SORTED

data: it_tab type sorted table of <>.

Reward points if helpful

Read only

0 Likes
1,579

but i don't want to use 'SORT' word into my program then how can i do?

Read only

Former Member
0 Likes
1,579

Hi Megha,

Try this

TYPES : BEGIN OF w_itab,

key1(4) TYPE c,

data(8) TYPE c,

END OF w_itab.

DATA: itab TYPE SORTED TABLE OF w_itab WITH HEADER LINE WITH UNIQUE KEY key1.

Regards,

Amit

Read only

Former Member
0 Likes
1,579

Hi,

Check the below addition of APPEND verb.

APPEND [wa TO] itab SORTED BY f.

<b>Effect</b>

Inserts the new entry into table and re-sorts the table by the sub-field f in descending order. This only makes sense if the table was sorted beforehand. When the number of table entries reaches the INITIAL SIZE parameter value, the last entry is deleted if the value f of a new entry is greater (particularly suitable for ranked lists). You can only sort by one component, and may only use this variant with a STANDARD TABLE.

If you specify wa TO, the new line is taken from the contents of the explicitly specified work area wa. Otherwise, it comes from the header line of the internal table itab.

<b>Example</b>

TYPES: BEGIN OF COMPANIES_TYPE,

NAME(10), SALES TYPE I,

END OF COMPANIES_TYPE.

DATA COMPANIES TYPE STANDARD TABLE OF COMPANIES_TYPE

INITIAL SIZE 3,

WA_COMPANIES TYPE COMPANIES_TYPE.

WA_COMPANIES-NAME = 'big'.

WA_COMPANIES-SALES = 90.

APPEND WA_COMPANIES TO COMPANIES.

WA_COMPANIES-NAME = 'small'.

WA_COMPANIES-SALES = 10.

APPEND WA_COMPANIES TO COMPANIES.

WA_COMPANIES-NAME = 'too small'.

WA_COMPANIES-SALES = 5.

APPEND WA_COMPANIES TO COMPANIES.

WA_COMPANIES-NAME = 'middle'.

WA_COMPANIES-SALES = 50.

APPEND WA_COMPANIES TO COMPANIES SORTED BY SALES.

The table now has three (-> INITIAL SIZE 3) entries. The line with the contents 'too small' in the sub-field NAME is deleted from the table because the entry for 'middle' has a greater value in the component SALES. This entry now appears in the second table line (after 'big' and before 'small').