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 internal table

Former Member
0 Kudos

Hi experts,

Kindly help very very urgent if i give sort itab(internal table) without mentioning fields in what basis the internal table will get sorted please very urgent

(Also, tell me the case when the table from which it is fetched doesn't contain any key fields)

please help

7 REPLIES 7

Former Member
0 Kudos

Hi,

SORT itab.

The entries in the internal table are sorted in ascending order using the key from the table definition (DATA, TYPES).

Reward if helpful.

krishnendu_laha
Active Contributor
0 Kudos

Hi Madhangi,

SAP does not appreciate sort by table without giving any field name specifically.

because main purpose of sorting is reading this table later in program using binary search. if you are not giving any field name; if you are selecting with key fields then it will sort by key fields otherwise it will choose arbitarly.

Hope it will help you.

Regards,

Krishnendu

Former Member
0 Kudos

From the help for SORT:

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.

Rob

Former Member
0 Kudos

Hi,

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. <b>The default is ascending.</b>

Regards

Sudheer

Former Member
0 Kudos

Hi..

Plz check the code below.



data:  begin of itab occurs 0,
         f1(10) type c,
         f2(10) type c,
       end of itab.

itab-f1 = '3'.
itab-f2 = 'S'.
append itab.

itab-f1 = '2'.
itab-f2 = 'T'.
append itab.

itab-f1 = '4'.
itab-f2 = 'S'.
append itab.

itab-f1 = '4'.
itab-f2 = 'P'.
append itab.

itab-f1 = '2'.
itab-f2 = 'S'.
append itab.

loop at itab.
  write:/ itab-f1, itab-f2.
endloop.

sort itab.

skip.
write:/ 'Sorted'.
loop at itab.
  write:/ itab-f1, itab-f2.
endloop.

You can get from the code that.. if u dont specify fields for sorting then SORT staement sort the itab by all the fields ranging from first field to last field.

Output of the above code is :

3 S

2 T

4 S

4 P

2 S

Sorted

2 S

2 T

3 S

4 P

4 S

Reward if useful

Regards

Prax

Former Member
0 Kudos

Your statement sorts the ITAB by all the fields in the internal table.

Regards,

Pavan

Former Member
0 Kudos

Hi,

Sorts the table in Ascending order.

Br,

Laxmi