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

Sorting

Former Member
0 Likes
967

Hi,

What is stable sorting?

What happens actually when i sort an internal table in stable mode?

Pls explain me clearly...

7 REPLIES 7
Read only

Former Member
0 Likes
928

Hi,

you can check the same by using F1 key also in SE38, write --> sort and press F1 Key :

...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.

Regards,

Sandeep Kaushik

Read only

Former Member
0 Likes
928

Hi,

Allows you to perform a stable sort, that is, the relative sequence of lines that are unchanged by the sort is not changed. If you do not use the STABLE option, the sort sequence is not preserved. If you sort a table several times by the same key, the sequence of the table entries will change in each sort. However, a stable sort takes longer than an unstable sort.

Reward if useful!

Read only

Former Member
0 Likes
928

Hi

see the doc

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.

Regards

anji

Read only

former_member189629
Active Contributor
0 Likes
928

Shori,

Sorting literally means SORTing... If u sort an internal table on the "Name field" for example, the table is sorted in alpha order of the name. U can use the ASCENDING/DESCENDING options too. If you do not use the STABLE option, the sort sequence is not preserved.

Reward if helpful,

Karthik

Read only

Former Member
0 Likes
928

Hi,

<b>

SORT itab... STABLE.</b>

allows you to perform a stable sort, that is, the relative sequence of lines that are unchanged by the sort is not changed. If you do not use the STABLE option, the sort sequence is not preserved. If you sort a table several times by the same key, the sequence of the table entries will change in each sort. However, a stable sort takes longer than an unstable sort.

Look at the demo Program in SAP --> <b>demo_int_tables_sort_stable</b>

Regards

Sudheer

Read only

Former Member
0 Likes
928

hi

DATA:

BEGIN OF ITAB OCCURS 0,

NAME(1),

TOTAL TYPE I,

END OF ITAB.

data: W_NAME(4),w_TOTAL(5).

ITAB-NAME = 'a'. ITAB-TOTAL = 480. APPEND ITAB.

ITAB-NAME = 'b'. ITAB-TOTAL = 470. APPEND ITAB.

ITAB-NAME = 'c'. ITAB-TOTAL = 470. APPEND ITAB.

ITAB-NAME = 'd'. ITAB-TOTAL = 470. APPEND ITAB.

ITAB-NAME = 'e'. ITAB-TOTAL = 460. APPEND ITAB.

w_NAME = 'NAME'.

W_TOTAL = 'TOTAL'.

sort itab by (W_NAME) (W_TOTAL).

LOOP AT ITAB.

WRITE:/ ITAB-NAME,ITAB-TOTAL.

ENDLOOP.

if u want to sort by total only, just clear the contents of W_NAME like CLEAR W_NAME.

Reward points if useful, get back in case of query...

Read only

Former Member
0 Likes
928

hi

good

Sorting Internal Tables

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

The larger the sort key, the more time the system needs to sort the table. If the sort key contains an internal table, the sorting process may be slowed down considerably.

You cannot sort a sorted table using the SORT statement. The system always maintains these tables automatically by their sort order. If an internal table is statically recognizable as a sorted table, the SORT statement causes a syntax error. If the table is a generic sorted table, the SORT statement causes a runtime error if the sort key is not the same as an extract of the beginning of the table key, you sort in descending order, or use the AS textaddition. In other words, the SORT statement is only allowed for generic internal tables, if it does not violate the internal sort order.

http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb3800358411d1829f0000e829fbfe/content.htm

reward point if helpful.

thanks

mrutyun^