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

Former Member
0 Likes
4,816

Hi Folks,

I have a small doubt regarding internal table. if we declare multiple fields in sort statement, how the internal table be sorted. i mean which field will be sorted first.

suppose if we write a statemetn like this,

SORT ITAB BY FLDAT CARRID CONNID.

How the sort mechanism happens for the above statement.

Thanks in advance,

Ram.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,960

Hi,

ITAB will be soted in ascending order first by the field FLDAT CARRID CONNID i.e. the order you've placed them.

Hope this helps.

Reward if helpful.

Regards,

Sipra

12 REPLIES 12
Read only

Former Member
0 Likes
2,961

Hi,

ITAB will be soted in ascending order first by the field FLDAT CARRID CONNID i.e. the order you've placed them.

Hope this helps.

Reward if helpful.

Regards,

Sipra

Read only

Former Member
0 Likes
2,960

first it will sort according to FLDAT if they are equal then it will sort by CARRID if they are equal then it will sort by CONNID.

Read only

Former Member
0 Likes
2,960

Hi,

If you give the sort statement in this mannar then it will sort CONNID first then carrid then fldat.

thanks

Sarada

Read only

Former Member
0 Likes
2,960

Hi,

SORT:

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

So the order is :

CARRID CONNID FLDAT

Reward Points if found helpfull..

Cheers,

Chandra Sekhar.

Read only

Former Member
0 Likes
2,960

Hi,

suppose the records are like below


num1  txt1    num2  txt2
123   abcd    876   text1
123   abcd    876   text2
123   abcd    875   text3
123   abcd    876   text4
123   abce    876   text5

if u say

sort itab by num1 txt1 num2 txt2

then it will become like this.


num1  txt1    num2  txt2
123   abcd    875   text3
123   abcd    876   text1
123   abcd    876   text2
123   abcd    876   text4
123   abce    876   text5

first it will be sorted based on num1.

then it will sort the records by txt1 which has same num1 value.

...

...

rgds,

bharat.

Read only

Former Member
0 Likes
2,960

The sort order depends on the sequence of the fields specified in the statement....

In your case it will sort based on

FLDAT - as First level sort

CARRID - as second level sort ( if more than one carrid records occured in the same FLDAT)

CONNID - as third level sort (if more than one connid records occured in the same FLDAT, CARRID)

Read only

Former Member
0 Likes
2,960

Hi,

SORT ITAB BY FLDAT CARRID CONNID

Sorting for the above takes place in the order in which u hav given the fields.

Sorting is generally done based on the key field.

Reward if Helpful

Thanks & Regards

Jagadish

Read only

Former Member
0 Likes
2,960

Hi Ram,

When ever we select data from data base table to internal table it may/maynot come in sorted order by primary keys of the table. Same SELECT Query may return different order of records for different executions.To ensure that our records in internal table are in proper order before using them we will use the statement SORT. Also if u want to arrange the records of the internal table based on non primary keys or combination of primary and nonprimary keys then we will use sort statement.

Take an example. U have been given a bunch of papers

having page numbers on each paper and u need to find some

page having page number 123. IF ur bunch is not arranged as

per the page number then it will be very difficult for u to find

out where page 123 will be. But if it is sorted by page number

and given then u can easily locate the page by searching the

pages in between 100-150.

SORT plays an important role when u are using

READ TABLE WITH BINARY SEARCH. Never Read an internal table with out binary search if ur table has more than 100 records. It will degrade the performance a lot. And it is mandatory to sort the internal table by the same fields which are used in READ TABLE WITH KEY addition. Also BINARY SEARCH Will not work if u sort the table by descending order.

Coming to sorting table before loop, If u are using any AT events in the loop like AT NEW, AT END OF etc then it is mandatory to sort the internal table by the field used in the AT EVENT.

Kindly Reward Points If You Found The Reply Helpful,

Cheers,

Chaitanya.

Read only

Former Member
0 Likes
2,960

Hi,

look at this Docu of statement SORT. You can get this by pressinf F1 on the statement SORT:

Syntax Diagram

SORT itab

Syntax

SORT itab

{ { ASCENDING

[BY {comp1 ASCENDING }

{comp2 ASCENDING }

... ] }

| { } }.

Extras:

1. ... STABLE

2. ... ASCENDING|DESCENDING

3. ... AS TEXT

4. ... BY compi ASCENDING

5. ... BY (otab)

Effect:

This statement sorts an internal table itab. As standard, numeric and byte-type components are sorted by their value and character-type components according to their binary representation code page). Textual sorting of character-type components is performed using the addition AS TEXT.

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.

For itab, a standard table or a hashed table is expected. Sorted tables cannot be sorted using SORT and the application of the SORT statement on sorted tables is syntactically prohibited. If the system does not notice until runtime that a sorted table is to be sorted, an untreatable exception occurs if this could result in change in the existing sorting. The latter occurs in the following cases:

If the BY addition is used to enter a different addition than 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 entered as a component in the BY addition.

Otherwise, the SORT statement is ignored for sorted tables.

Addition 1

... STABLE

Effect:

STABLE is used to perform stable sorting, which means that the relative sequence of lines that does not change in the sort key remains unchanged in the sort. Without the STABLE addition, the sequence is not retained and multiple sorting of a table using the same sort key results in a different sequence each time the table is sorted.

Addition 2

... ASCENDING|DESCENDING

Effect:

The addition ASCENDING or DESCENDING can be used to specify the sort direction explicitly as ascending or descending. If neither of the additions is entered, the table is sorted in ascending order. This sort direction can be overwritten after the BY addition for components that are individually listed there.

Addition 3

... AS TEXT

Effect:

The addition AS TEXT specified 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 encoding in the code page of the current text environment. This specification can be overwritten after the BY addition for the components that are individually listed there. The text environment is set when an internal session is opened or by using the statement SET LOCALE.

Notes:

Sorting without the AS TEXT addition is considerably faster than when the addition is used. If text-type components only contain characters of the ASCII character set, both sorts produce the same result and the AS TEXT addition is not required.

The result of a lexicographical sort depends on the operating system of the application server. Although the sequence of individual letters that belong to the activated language remains the same across different operating systems, there are differences in terms of the characters that do not belong to the alphabet of the activated language. Even if only the letters are used from the alphabet of the activated language, some slight differences occur. Furthermore, the sequence of upper and lower case letters is operating system-dependent.

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

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

Example:

Sorting a hashed table text_tab by alignment in the code page and according to the locale of the current text environment. If a Western European text environment is set, the sorts result in the sequences Miller, Moller, Muller, Möller and Miller, Moller, Möller, Muller respectively. (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 .

ENDLOOP.

SKIP.

ENDFORM.

Addition 4

... BY compi ASCENDING

Effect:

With the addition BY compi, the table is not sorted according to the table key, but instead according to the components comp1 comp2... that are specified after it. The components are specified as described under specifying components, with the restriction that the number of components that can be specified is restricted to 250. In purely dynamic component specification, if all names name only contain blank characters, no sort takes place. The priority of the sort depends on the order in which the components comp1 comp2 ... are specified from left to right.

If neither of the additions ASCENDING or DESCENDING is specified after comp1 comp2 ..., the sort direction specified by addition 2 is used. If one of the additions ASCENDING or DESCENDING is specified, it overwrites the specification for this component.

If the AS TEXT addition is not specified after a text-type component comp1 comp2..., the specification defined by addition 3 is used. If the AS TEXT addition is specified after a text-type component, it overwrites the specification for this component. In non-text-type components, AS TEXT cannot be specified.

In internal tables with header lines, field symbols can also be entered for comp1 comp2 ... outside classes. If a component of the header line or the whole header line is assigned to the field symbol when the statement is executed, the table is sorted according to the corresponding component or the whole line. If no data object is assigned to a field symbol, the entry is ignored. If a different data object is assigned to a field symbol, an untreatable exception occurs.

Notes

As of release 7.0, instead of individual dynamic components, the internal table otab can be entered as a dynamic sort key (see addition 5). Using this table has the advantage that any exceptions are treatable. When specifying the table, the number of components of the sort key is also dynamic. In contrast, when individual dynamic components are used, a character-type data object must be specified for every component that may be required, which is not taken into account in the sort if it only contains blank characters.

Instead of using field symbols for dynamic component specifications, it is recommended to specify bracketed character-type data objects or an internal table (as of release 7.0), which contain the name of the components.

Addition 5

... BY (otab)

Effect:

With the addition BY (otab), the table is not sorted according to the table key, but instead according to the component specified dynamically in the internal table otab (as of release 7.0). Each line of the table otab defined a component of the sort key. The priority of the sort is based on the order of the lines in otab. A maximum of 250 components can be specified. If the table otab is initial, the table is not sorted.

For otab, a standard table of the table type ABAP_SORTORDER_TAB from the ABAP Dictionary must be specified. The line type of this table is the Dictionary structure ABAP_SORTORDER with the following components:

NAME of type SSTRING

for specifying a component of the sort key. The component is entered in the form "comp_name[off(len)]", whereby "comp_name" must be the name of a component contained in itab in upper case to which an offset/length specification "off(len)" can be appended. "off" and "len" must be suitable numeric values.

DESCENDING of type CHAR of length 1 for specifying the

sort direction for the current component. If DESCENDING is initial, the sort is performed in ascending order. If DESCENDING has the value "X", the table is sorted in descending order.

ASTEXT of type CHAR of length 1

for textual sorting of the current component. If ASTEXT has the value "X", the sort is performed as with the AS TEXT addition. This is only possible for character-type components. If ASTEXT is initial, character-type components are sorted according to their binary representation.

If a column of otab, has invalid content (NAME contains the name of a component that does not exist or an incorrect offset/length specification, DESCENDING and ASTEXT do not contain "X" or the initial value), this leads to a treatable exception of the class CX_SY_DYN_TABLE_ILL_COMP_VAL.

Notes:

The addition BY (otab) cannot be combined with BY compi.

When using the addition BY (otab), it is not possible to use DESCENDING or AS TEXT to specify a descending sort direction or textual sorting for all components.

If a single bracketed data object (dobj) is specified after the BY addition, its data type decides whether its content is used to specify a single component or multiple components. In either case, no sort takes place if dobj is initial.

Example:

Dynamic import of a database table into a dynamic internal table and dynamic sorting of its content. The name of the database table and the names of the columns by which the table is to be sorted can be entered on a selection screen.

PARAMETERS dbtab TYPE c LENGTH 30.

SELECT-OPTIONS columns FOR dbtab NO INTERVALS.

DATA: otab TYPE abap_sortorder_tab,

oline TYPE abap_sortorder,

dref TYPE REF TO data.

FIELD-SYMBOLS: BY (otab).

CATCH cx_sy_dyn_table_ill_comp_val.

MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.

LEAVE PROGRAM.

ENDTRY.

Exceptions

Non-Catchable Exceptions

CX_SY_DYN_TABLE_ILL_LINE_TYPE

Cause: The table otab has a prohibited line type.

Runtime Error: DYN_TABLE_ILL_LINE_TYPE

CX_SY_DYN_TABLE_ILL_COMP_VAL

Cause: A column of the table otab contains a prohibited value.

Runtime Error: DYN_TABLE_ILL_COMP_VAL

Non-Catchable Exceptions

Cause: A sort criterion dynamically specified in the form (name) with the explicit addition AS TEXT is not of the type C or W.

Runtime Error: SORT_AS_TEXT_BAD_DYN_TYPE

Cause: A field symbol used as a dynamic sort criterion with an explicit addition AS TEXT is not of the type C or W.

Runtime Error: SORT_AS_TEXT_BAD_FS_TYPE

Cause: A field symbol used as a dynamic sort criterion does not point to the header row of the internal table to be sorted.

Runtime Error: SORT_ITAB_FIELD_INVALID

Cause: For a table of the type SORTED TABLE, the sort key does not match a beginning piece of the table key.

Runtime Error: SORT_SORT_ILL_KEY_ORDER

Cause: The additions DESCENDING and AS TEXT are not allowed for tables of the type SORTED TABLE.

Runtime Error: SORT_SORT_ILLEGAL

Cause: More than 250 sort criteria.

Runtime Error: SORT_TOO_MANY_FIELDS

Cause: Sort mode neither 'E'(xternal) nor 'I'(nternal)

Runtime Error: SORT_ILLEGAL_MODE

Regards, Dieter

Read only

Former Member
0 Likes
2,960

Hi everybody,

i got mixed answer from all of you. now i am confused a lot to judge which is the correct answer.

Read only

0 Likes
2,960

just try the same example and see first it will go first with fldat and then carrid and then connid

Read only

Former Member
0 Likes
2,960

sorting would be in this way

first whole internal table would be considered and would be sorted according to firtst sorting criteria( first sorting field) than other recrods will pe sorted according by taking consideration first soting criteria eg of this is lets have this internal table before sorting

bb aa dd cc row 1

aa cc dd bb row 2

cc dd bb cc row 3

aa bb dd cc row 4

now what will happen first table will be sorted according to first field

aa row2

aa row 4

bb

cc now what will happen is second sorting will be done on row 2 and 4 only

aa bb row 4

aa cc row 2 now third condition will be taken into consideration and final result would be

aa bb dd cc

aa cc dd bb

bb aa dd cc

cc dd bb cc

award point if help ful