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

To sort an Internal table

Former Member
0 Likes
5,104

Hi,

In an Internal table I want to sort one field by ascending and the very next field by descending.

for example: Internal table has fields MATNR and BUDAT.

MATNR by ascending and BUDAT by descending.

For the group of MATNR(should be in ascending order),i want to get all the BUDAT in descending order.

Can u pls help on this..

Regards,

Raja

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,388

Sorts the lines of the selection.

Syntax

... ORDER BY PRIMARY KEY |... <si> [ASCENDING|DESCENDING]...

Sorts the selection in ascending or descending order according to the primary key or the contents of the fields listed.

then

finaly sort with next field

SORT itab by second field

Girish

6 REPLIES 6
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
1,388

Hi,

You can do that as follows

SORT ITAB BY FIELD1 ASCENDING FIELD2 DESCENDING.

For your query you can use

GROUP BY MATNER

and

ORDER BY BUDAT in the select query itself.

Regards,

Sesh

Read only

shalini_rn
Associate
Associate
0 Likes
1,388

Hi Raja..

If we assume the name of internal table as mat_tab then use the following code:

sort mat_tab by MATNR ascending BUDAT descending.

Rgds

Shalini

Read only

Former Member
0 Likes
1,388

HI,

see this example

DATA:ITAB LIKE MARA OCCURS 0 WITH HEADER LINE.

SELECT * FROM MARA INTO TABLE ITAB.

LOOP AT ITAB.

WRITE:/ ITAB-MATNR,ITAB-MTART.

ENDLOOP.

SORT ITAB BY MTART ASCENDING matnr descending .

SKIP.

LOOP AT ITAB ."WHERE MTART = 'FERT'.

WRITE:/ ITAB-MATNR,ITAB-MTART.

ENDLOOP.

rgds,

bharat.

Read only

Former Member
0 Likes
1,388

hi,

divide the fields from one internal table to 2tables with one key field then U can sort one assending and one desending...... while listing the records check the condition key fields should be satisfied.....

reward points if helpful,

Regards

Raghunath.S

Read only

Former Member
0 Likes
1,388

Hi Bala,

Sort internal table by Budat first in descending order ,

then sort that internal table by Matnr in ascending order.

This will help U.

Regards,

Sree.

PS: reward points if Useful.

Read only

Former Member
0 Likes
1,389

Sorts the lines of the selection.

Syntax

... ORDER BY PRIMARY KEY |... <si> [ASCENDING|DESCENDING]...

Sorts the selection in ascending or descending order according to the primary key or the contents of the fields listed.

then

finaly sort with next field

SORT itab by second field

Girish