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

Former Member
0 Likes
802

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
Read only

Former Member
0 Likes
784

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.

Read only

Former Member
0 Likes
784

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

Read only

Former Member
0 Likes
784

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

Read only

Former Member
0 Likes
784

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

Read only

Former Member
0 Likes
784

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

Read only

Former Member
0 Likes
784

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

Regards,

Pavan

Read only

Former Member
0 Likes
784

Hi,

Sorts the table in Ascending order.

Br,

Laxmi