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

sorting internal table based on field value

Former Member
0 Likes
8,976

hi,

i wanna sort the internal table gt_bseg based on the posting key value.

that is if the posting key (bseg-bschl) is equal to 06 or 15, the particular record should become the last record of gt_bseg.

as i want to print this record after printing the total internal table so i want to make the record havong posting key as 06 or 15 as last record of itab.

please help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,562

hi

good

.

go through this and use accordinlgy

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

SORT <itab> [ASCENDING|DESCENDING] [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.

http://help.sap.com/saphelp_sm32/helpdata/en/fc/eb3800358411d1829f0000e829fbfe/content.htm

thanks

mrutyun^

4 REPLIES 4
Read only

Former Member
0 Likes
3,562

I think what you need to do is after you have printed your table then sort it by bschl and simply read the value you are looking for, and lastly print the record.

SORT gt_bseg BY bschl.
READ TABLE gt_bseg INTO lc_bseg WITH KEY bschl = '15'.
IF SY-SUBRC <> 0.
READ TABLE gt_bseg INTO lc_bseg WITH KEY bschl = '06'.
ENDIF.

Read only

former_member189059
Active Contributor
0 Likes
3,562

hello,

What you can do is to create a dummy field in your internal table

whenever bseg-bschl = 06 or 15 set the value of that field to something (eg: 1)

then sort the internal table by that dummy field

Read only

Former Member
0 Likes
3,563

hi

good

.

go through this and use accordinlgy

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

SORT <itab> [ASCENDING|DESCENDING] [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.

http://help.sap.com/saphelp_sm32/helpdata/en/fc/eb3800358411d1829f0000e829fbfe/content.htm

thanks

mrutyun^

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
3,562

Hi,

Create one more internal table of similar structure.

say it is itab1.

loop at itab into wa where bschl <> '06' and bschl <> '15.

append wa to itab1.

endloop.

loop at itab into wa where bschl = '06' .

append wa to itab1.

endloop.

loop at itab into wa where bschl = '15.

append wa to itab1.

endloop.

Now itab1 will records in the sorted order according to the requirement.

Kindly reward points by clicking the star on the left of reply,if it helps.