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 Itab

former_member202682
Participant
0 Kudos

Hello friends,

Can anyone tell me how will this statement behave

Sort itab by field1

where my itab has 2 fields 1 is customer no and 2 is description.

Now , here the sort is sorting the itab by fields 1 as well by description.

can anyone please tell me is this behavior normal for sap i:e after field one system will sort itab by field 2 also without mentioning in the SORT statement.

Please help.

Thanks n Regards,

AShwini

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Ashwini,

System will not automaticaly sort itab by field2 unless mentioned. It does happen if the entries in database tables are already in sorted form.

Else you need to mention both fields in sorting.

sort itab by field1 field2.

Regards,

Arati.

6 REPLIES 6

Former Member
0 Kudos

Hi Ashwini,

System will not automaticaly sort itab by field2 unless mentioned. It does happen if the entries in database tables are already in sorted form.

Else you need to mention both fields in sorting.

sort itab by field1 field2.

Regards,

Arati.

Former Member
0 Kudos

Hi

you can do that by

sort itab by field1 feild2

or

simply sort itab

Reward if usefull

Former Member
0 Kudos

Hi Ashwini,

Sort itab by field1

will sort the itab by field 1 i.e.,customer number.The other fields related to field1(i.e.description) also gets sorted automatically according to the key field (customer number).

thanks,

Shanmugapriya

rodrigo_paisante3
Active Contributor
0 Kudos

Hi,

when you use sort itab field1 field2 ... fieldn (you can use optional ascending or descending)

you specific the priority sort to your table.

Example:

Field1 Field2 Field3

A........104........2

A........105........2

A........106........6

B........107........7

B........108........7

when you just use sort itab by field, the table is sorted by this field.

Try to see the abap help about sort, there are useful samples here.

Regards.

Former Member
0 Kudos

Hi,

supposse the IT has the following records,

cust-id cust-desc

104 doctor

101 doctor

105 student

102 merchant.

SORT tbl_cust by cust-id.

then tbl_cust contain records like

cust-id cust-desc

101 doctor

102 merchant.

104 doctor

105 student

In above sort statement sorth the records a/c to cust-id in acending order.

SORT tbl_cust by cust-desc.

then tbl_cust contain records like

cust-id cust-desc

104 doctor

101 doctor

102 merchant.

105 student

In above sort statement the records a/c to cust-description in alphabetical order.

SORT tbl_cust by cust-desc cust-id.

then tbl_cust contain records like

cust-id cust-desc

101 doctor

104 doctor

102 merchant.

105 student

In above sort statement, the records first sort a/c to cust-desc

104 doctor

101 doctor

then it sort agian cust-id in acending order.then finally it sort into

101 doctor

104 doctor

hope you can understand with above examples.

<b>Reward with points if helpful.</b>

Regards,

Vijay

Former Member
0 Kudos

It may be the case that your DB table (for where you are working on the internal table) is a sorted table and the sorted field is by default Field 2.

Hence, in your case, its better to write the sort statement as :

<b>sort itab by field 1 field 2.</b>

Reward if helpful,

Thanks and Regards,

Anindita