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

Former Member
0 Likes
1,644

Can you tell how to write Sort statement in ABAP 4.6 Version

11 REPLIES 11
Read only

Sandeep_Panghal
Product and Topic Expert
Product and Topic Expert
0 Likes
1,584

Hi,

SORT by fieldname.

Hope this helps.

Read only

Former Member
0 Likes
1,584

hi,

You can write

SORT ITAB by f1 f2.

Read only

Former Member
0 Likes
1,584

SORT ITAB BY field1 field2

Read only

Former Member
0 Likes
1,584

sort itab by field.

Read only

Sandeep_Panghal
Product and Topic Expert
Product and Topic Expert
0 Likes
1,584

Here we have some more details:

SORT itab ASCENDING

.

Extras:

1. ... ASCENDING|DESCENDING

2. ... AS TEXT

3. ... STABLE

Effect

This statement sorts an internal table itab. As standard, numeric and byte-type components are sorted according to their value and character-type by their binary representation (code page). For text-based sorting of character-type components, you can use the addition AS TEXT.

For itab, a standard table or hashed table is expected. Sorted tables cannot be sorted using SORT. If no explicit sort key sort_key is specified, the internal table itab is sorted by the table key, whereby the table key can contain a maximum of 250 components. The priority of sorting is determined by the order in which the key fields are specified in the table definition. For the standard key, the priority of the sort depends on the order of the key fields in the line type of the table.

It is syntactically forbidden to use the SORT statement on sorted tables. If it is not determined until runtime that a sorted table is to be sorted, this leads to an untreatable exception if the sort could change the existing sorted order. This happens in the following cases:

If the sort key specified in sort_key is different to the initial part of the table key

If the addition DESCENDING is used

If the addition AS TEXT is used

If an attribute of an object is specified as a component in sort_key.

Otherwise the SORT statement is ignored for sorted tables.

Addition 1

... ASCENDING|DESCENDING

Effect

You can use the additions ASCENDING or DESCENDING to explicitly define whether the table is sorted in ascending or descending order. If neither addition is specified, the data is sorted in ascending order by default. This sort order can be overwritten for individual components in the explicit sort key sort_key.

Addition 2

... AS TEXT

Effect

The addition AS TEXT specifies that text-type components are sorted according to the locale of the current text environment. If AS TEXT is not specified, text-type components are sorted according to the coding in the code page of the current text environment. This specification can be overwritten for individual text-type components in the explicit sort key sort_key. The text environment is set when an internal session is opened, or using the statement SET LOCALE.

Notes

Sorting without the AS TEXT addition is considerably faster than sorting using the addition. If text-type components only contain characters of the ASCII character set, both sort options result in the same sort order and the addition AS TEXT is not necessary.

The result of lexicographic sorting depends on the operating system of the application server. While the sequence of the individual letters that belong to the activated language is the same for all operating systems, there are differences in the characters that do not belong to the alphabet of the activated language. When sorting whole words, there are also slight differences even if only using letters from the alphabet of the activated language. The order of upper and lower case letters also depends on the operating system.

For the AS TEXT addition to function correctly, the profile parameter install/collate/active must not have the value 0.

The use of the AS TEXT addition normally renders the statement CONVERT TEXT superfluous.

Addition 3

...STABLE

Effect

You can use STABLE to ensure a stable sort order. This means that the order of lines, which is the same in the sort key, remains unchanged after sorting. Without the STABLE addition, the order is not preserved and repeated sorting of a table by the same sort key changes the order each time the table is sorted.

Example

Sorting a hashed table text_tab by the order in the code page and according to the locale of the current text environment. If a Western European text environment is set, sorting results in the orders Miller, Moller, Muller, Möller, or Miller, Moller, Möller, Muller (also see the example for SET LOCALE).

DATA text_tab TYPE HASHED TABLE OF string

WITH UNIQUE KEY table_line.

INSERT: `Muller` INTO TABLE text_tab,

`Möller` INTO TABLE text_tab,

`Moller` INTO TABLE text_tab,

`Miller` INTO TABLE text_tab.

SORT text_tab.

PERFORM write_text_tab.

SORT text_tab AS TEXT.

PERFORM write_text_tab.

FORM write_text_tab.

FIELD-SYMBOLS TYPE string.

LOOP AT text_tab ASSIGNING .

WRITE / .

ENDLOOP.

SKIP.

ENDFORM.

Read only

Former Member
0 Likes
1,584

Hi,

1.Sory itab BY f1 f2 ... fn

2. ... ASCENDING

3. ... DESCENDING

4. ... AS TEXT

5. ... STABLE

regards,

Kumar

Read only

Former Member
0 Likes
1,584

now u have internal table with fields matnr, maktx, werks etc....

say Matnr is primary key along with werks...So, need to sort on these fields for further processing.

sort <internaltable> by matnr werks,

Read only

Former Member
0 Likes
1,584

SORT table name BY fields(in that table).

ex: sort it_vbak by vbeln.

Read only

Former Member
0 Likes
1,584

Hi Rajasekhar,

welcome to SDN.

basic systax of sort is

SORT itab.

with following addition

1. ... BY f1 f2 ... fn (field names)

2. ... ASCENDING

3. ... DESCENDING

4. ... AS TEXT

5. ... STABLE

hope this helps .

award forum points to helpful answers

Read only

Former Member
0 Likes
1,584

Got the solution

Read only

Former Member
0 Likes
1,584

Hi,

sort itab by <fields>

Thanks

Vikranth khimavath