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

SORT statement

Former Member
0 Likes
9,447

Hi,

I have one query in sort statement.

Suppose in internal table ITAB having 4 fields LIFNR, LAND1, NAME1 & LV_TEXT.

So, what is the diff b/w

SORT ITAB.

Is it will sort the table ITAB based on all fields?? or I need to write the below statement.

SORT ITAB BY LIFNR LAND1 NAME1 & LV_TEXT.

Pls revert me asap.

7 REPLIES 7
Read only

Former Member
0 Likes
4,517

Hi Neha,

If no explicit sort key is entered using the BY addition, the internal table itab is sorted according to the table key. In this case, the table key can contain a maximum of 250 components. The priority of the sort is based on the order in which the die key field are specified in the table definition. In standard keys, the sort is prioritized according to the order of the key fields in the line type of the table.

Regards,

Sai

Read only

Former Member
0 Likes
4,517

Hi

SORT ITAB

Table is sorted by numeric and byte-type components are sorted according to their value and character-type by their binary representation.

SORT ITAB by fields sorted by specific fields mentioned .

better to use this always.

Regards

Anji

Read only

Former Member
0 Likes
4,517

SORT ITAB will create a performance issue as it will take more time and sort table by all fields as per colman

SORT ITAB BY LIFNR LAND1 NAME1 <b>& LV_TEXT</b>

I think lv_text is some field in itab

In this case it will take less time to sort be..

first it will sort by LIFNR

then check for same multiple entries in LIFNR then sort by LAND1

and so on.......

So time is less

so better to allways use SORT BY

Rewards if useful..............

Minal

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
4,517

Hi,

By default if you dont mention any fields in SORT BY then the sorting will happen based on the KEY of the internal table. The KEY that you mention when you declare your internal table, if you dont mention any key then all character type fields are considered as the KEY FIELDS.

So SORT itab will sort by KEY of the internal table(uses KEY if it is defined else all character type fields are part of KEY).

When you mention the fields after SORT BY Then they will be used in the same order as they mentioned.

SO in your case it depends if you have mentioned any key for the internal table.

Regards,

Sesh

Read only

varma_narayana
Active Contributor
0 Likes
4,517

Hi Neha..

If you don't specify any Key fields while declaring the ITAB, Then both these statements work same way.

i.e the SORT is done on all the fields.

<b>reward if Helpful.</b>

Read only

Former Member
0 Likes
4,517

hi,

SORT statement is used to sort internal table based on some condition. if noting is mentioned then the table is sorted according to key fields of that table.

for ex:

sort itab.

sorts internal table by taking key fields of database table into consideration.

so to sort table according to our wish we can give the list of values to sort as by using BY ADDITION to SORT statement

as

sort itab by matnr, werks,......... [ASC/DEC]

SORTS THE TABLE BASED ON THE FIELD LIST GIVEN BY U. if nothing is mentioned it will sort in ascending order for decending order to records u have to give it explicitly.

if helpful reward some points.

with regards,

Suresh Aluri.

Read only

Former Member
0 Likes
4,517

Hi Neha,

Please try to help yourself and use the on-line ABAP help before asking questions like this. If you put your cursor on the SORT command in your ABAP editor and press F1 you will see the SAP help for the command SORT. This should explain how it works.

If you still don't understand fully, try searching this forum - these questions have been asked numerous times before.

Gareth.