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

Reg: Sorting the Internal table

Former Member
0 Likes
3,255

Hi All,

MY requirement was to sort the internal table based on some fields Entered by the user.

Ex: If In my internal table consist of MATNR, MTART,WERKS, LGORT field values.

If the value of v_matnr = MATNR and V_WERKS = WERKS.

How we will sort the internal table based on MATNR and WERKS.

I used field symbols but its not sorting the internal table.

Please help.

Regards,

Sreedhar.T

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,377

Check the code below

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

Cheers!!!

13 REPLIES 13
Read only

Former Member
0 Likes
2,377

hi

just the sort functionality

ex:

sort int_tab by the field names

see the description below :

SORT itab.

Additions:

1. ... BY f1 f2 ... fn

2. ... ASCENDING

3. ... DESCENDING

4. ... AS TEXT

5. ... STABLE

In an ABAP Objects context, a more severe syntax check is performed that in other ABAP areas. See Field symbols not allowed as sort criterion.

Effect

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

Addition 1

... BY f1 f2 ... fn

Effect

Uses the sort key defined by the sub-fields f1, f2, ..., fn of the table itab instead of the table key. The fields can be of any type; even number fields and tables are allowed.

You can also specify the sort fields dynamically in the form (name). If name is blank at run time, the sort field is ignored. If itab is a table with a header line, you can also use a field symbol pointing to the header line of itab as a dynamic sort criterion. A field symbol that is not assigned is ignored. If a field symbol is assigned, but does not point to the header line of the internal table, a runtime error occurs.

If the line type of the internal table contains object reference variables as components, or the entire line type is a reference variable, you can use the attributes of the object to which a reference is pointing in a line as sort criteria (see Attributes of Objects as the Key of an Internal Table

You can address the entire line of an internal table as the key using the pseudocomponent TABLE_LINE. This is particularly relevant for tables with a non-structured line type when you want to address the whole line as the key of the table (see also Pseudocomponent TABLE_LINE With Internal Tables).

If you use one of the additions 2 to 5 before BY, it applies to all fields of the sort key by default. You can also specify these additions after each individual sort field f1, f2, ..., fn. For each key field, this defines an individual sort rule which overrides the default.

Addition 2

... ASCENDING

Effect

Sorts in ascending order. This is also the default if no sort order is specified directly after SORT. For this reason, it is not necessary to specify ASCENDING explicitly as the default sort order.

With the addition BY, you can also specify ASCENDING directly after a sort field to define ascending order explicitly as the sort sequence for this field.

Addition 3

... DESCENDING

Effect

Sorts in descending order. If the addition comes right after SORT, DESCENDING is taken as the default for all fields of the sort key.

With the addition BY, you can also specify DESCENDING directly after a sort field.

Addition 4

... AS TEXT

Effect

Text fields are sorted appropriate to the locale. This means that the relative order of characters is defined according to the text environment being used.

When an internal mode is opened (in other words, when a roll area is opened), the text environment is automatically set to the logon language specified in the user master record. If necessary, however, you can change the text environment explicitly in your program by using a SET-LOCALE statement.

If the addition comes directly after itab, locale-specific rules are used for all fields of the sort key where the type of these fields is C or W. After the sort, the sequence of entries usually does not match the sequence which results otherwise, without using the addition AS TEXT, i.e. with binary sorting.

With the addition BY, you can also specify AS TEXT directly after a sort field, provided it is of type C or W, or a structured type. Otherwise, a runtime error occurs. In sort fields with a structured type, AS TEXT only affects subcomponents with type C or W.

Note

Note the rules for locale-specific sorting.

Example

Sort a name table with different keys:

TYPES: BEGIN OF PERSON_TYPE,

NAME(10) TYPE C,

AGE TYPE I,

COUNTRY(3) TYPE C,

END OF PERSON_TYPE.

DATA: PERSON TYPE STANDARD TABLE OF PERSON_TYPE WITH

NON-UNIQUE DEFAULT KEY INITIAL SIZE 5,

WA_PERSON TYPE PERSON_TYPE.

WA_PERSON-NAME = 'Muller'. WA_PERSON-AGE = 22.

WA_PERSON-COUNTRY = 'USA'.

APPEND WA_PERSON TO PERSON.

WA_PERSON-NAME = 'Moller'. WA_PERSON-AGE = 25.

WA_PERSON-COUNTRY = 'FRG'.

APPEND WA_PERSON TO PERSON.

WA_PERSON-NAME = 'Möller'. WA_PERSON-AGE = 22.

WA_PERSON-COUNTRY = 'USA'.

APPEND WA_PERSON TO PERSON.

WA_PERSON-NAME = 'Miller'. WA_PERSON-AGE = 23.

WA_PERSON-COUNTRY = 'USA'.

APPEND WA_PERSON TO PERSON.

SORT PERSON.

Now, the sequence of the table entries is as follows:

Miller 23 USA

Moller 25 FRG

Muller 22 USA

Möller 22 USA

If, for example, you apply German sort rules where the umlaut comes directly after the letter 'o' in the sort, the data record beginning with 'Möller' would not be in the right place in this sequence. It should come second.

Provided a German-language locale is set (e.g. sorting is according to German grammatical rules, see also SET LOCALE), you can sort the names according to German rules as follows:

SORT PERSON BY NAME AS TEXT.

Now, the sequence of table entries is as follows:

Miller 23 USA

Moller 25 FRG

Möller 22 USA

Muller 22 USA

Further examples:

SORT PERSON DESCENDING BY COUNTRY AGE NAME.

Now, the sequence of table entries is as follows:

Miller 23 USA

Möller 22 USA

Muller 22 USA

Moller 25 FRG

SORT PERSON DESCENDING BY AGE ASCENDING NAME AS TEXT.

Now, the sequence of table entries is as follows:

Muller 22 USA

Möller 22 USA

Miller 23 USA

Moller 25 FRG

Addition 5

... STABLE

Effect

Uses a stable sort, that is, the relative sequence of entries that have the same sort key remains unchanged.

Unlike additions 2 to 4, you cannot use this addition directly after a sort field.

Notes

General:

The number of sort fields is restricted to 250.

The sort process is only stable if you use the STABLE addition. Otherwise, a predefined sequence of fields used to sort a list is not usually retained.

It does not make sense to use the SORT command for a SORTED TABLE. If the table type is statically declared, the system returns a syntax error if you try to SORT the table. If the table type is not statically declared (for example, because the table was passed to a FORM routine as an INDEX TABLE in a parameter), and the system can interpret the SORT statement as an empty operation, it ignores the statement. This is the case when the key in the BY clause corresponds to the beginning of the table key. Otherwise, a runtime error occurs.

To delete all duplicate entries from a sorted internal table (e.g. just after SORT), you can use the DELETE ADJACENT DUPLICATES FROM itab statement.

When using the addition AS TEXT, the sequence of entries after the sort does not usually match the sequence resulting from a binary sort, i.e. if the addition AS TEXT is not specified. The consequence of this is that after the SORT, you are not allowed to access with the READ TABLE itab ... BINARY SEARCH statement.

If you still want to access data sorted apppropriate to the locale with a binary search, you can do this by including an additional component in the table where you can explictly store the data formatted using the CONVERT TEXT ... INTO SORTABLE CODE statement. This is also recommended for performance reasons if you have to re-sort the table several times according to locale-specific criteria.

If the internal table has more than 2^19 lines or is larger than 12 MB, the system sorts it physically using an external auxiliary file. You can specify the directory in which the file should be created using the SAP profile parameter DIR_SORTTMP. By default, the system uses the SAP data directory (SAP profile parameter DIR_DATA).

Regards,

Prasant

*reward if useful

Read only

Former Member
0 Likes
2,377

Hi Gadde,

Try this,

sort <table> by <field1> ascending (or descending)

<field2> ascending (or descending)

Thanks,

Reward If Helpful.

Read only

Former Member
0 Likes
2,377

hi Sreeman,

try this.

after fetching the data into itab use below stmt

Sort itab by matnr werks

<b>reward points if useful</b>

Read only

Former Member
0 Likes
2,377

hi

sort itab by matnr werks ascending

it may works

Read only

0 Likes
2,377

Hi all,

My question was, if i know the fields we can sort by using

SORT i_final by MATNR werks.

But the field will be given by the user in the selection screen.

How do i know which fileds the user enters.

If i know the field name will be in some variable, How to sort the internal table from that variable

Read only

Former Member
0 Likes
2,377

Hi,

You can solve this problem with the help of a macro.

Write a macro as below -

define sort_internal_table.

sort &1 by &2 &3 ascending.

end-of-definition.

call this macro when ever you want to sort the internal table dynamically.

you can call it as below -

you want to sort it by matnr and werks and your internal table name is itab.

the call would be -

sort_internal_table itab matnr werks.

you can use it repeatedly for any fields on which you want to sort.

<b>Reward if useful.</b>

Regards,

Pritha

Read only

0 Likes
2,377

Hi,

Can you briefely give me the code..

If i have to sort the Internal table

I_FINAL, and i have the fields in variables v_first, v_second, v_third...

How can i write the code.. Since i dont have any idea on Macros..

Read only

0 Likes
2,377

leme kno da selection screen fields and da structure of internal table n i'll get bak 2 u

Read only

0 Likes
2,377

I have the Interna table

select(1) TYPE c,

linhi(8) TYPE c,

verid TYPE verid,

lrate(17) TYPE c,

matnr TYPE matnr,

xrflag TYPE zxrflag,

aufnr TYPE plnum,

wonum TYPE zwonum,

gsmng(17) TYPE c,

status(16) TYPE c,

serial TYPE zxrflag,

sdate TYPE psttr,

stime TYPE pstti,

edate TYPE pedtr,

etime TYPE pedti,

fflag TYPE plafx,

soitem(12) TYPE c,

crsd TYPE vbep-edatu,

lead TYPE vbap-zllt,

trlt TYPE marc-wzeit,

In the selection screen, for all these fields, user enters the Number 1,2,3 like that,

Based on that we have to sort the internal table

Read only

former_member188827
Active Contributor
0 Likes
2,377

try sumthing like dis:

PARAMETERS:zfield(2) type c.

types:begin of it,

z1 type i,

z2 type i, z3 TYPE i,

z4 TYPE i,

END OF it.

data itab TYPE TABLE OF it with HEADER LINE.

itab-z1 = 9.

itab-z2 = 6.

itab-z3 = 4.

itab-z4 = 23.

APPEND itab.

itab-z1 = 2.

itab-z2 = 1.

itab-z3 = 3.

itab-z4 = 33.

APPEND itab.

if zfield eq 'Z1'.

sort itab by z1.

elseif zfield eq 'Z2'.

sort itab by z2.

elseif zfield EQ 'Z3'.

sort itab by z3.

elseif zfield EQ 'Z4 '.

sort itab by z4.

endif.

LOOP AT ITAB.

WRITE: / itab-z1, itab-z2, itab-z3, itab-z4.

ENDLOOP.

plz reward points if dis helps

Read only

former_member188827
Active Contributor
0 Likes
2,377

with parameters statement, take input regarding da field on da basis of which sorting will b done..

den use if conditions to check which field was input by user and carry out sorting

Read only

2,377

*Macro
define sort_itab.
  sort &1 by &2 &3 ascending.
end-of-definition.

*sorting your table using the macro you created
sort_itab itab param1 param2.
Read only

Former Member
0 Likes
2,378

Check the code below

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

Cheers!!!