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 ITAB BY <VARIABLE_NAME>

naveenvishal
Contributor
0 Likes
658

hi,

i want to sort the int table with variable parameters, like VKBUR, BUDAT, VKORG, MATNR, etc.

for that ive created a char variable of 5 length (say CRIT) and assigned value based on selection criteria by the user.

Then, im using SORT ITAB BY CRIT. Its not getting sorted properly.

Any suggestions,

Regards,

Naveen

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
0 Likes
638

Field is provided dynamically so it must be enclosed in paranthesis.


SORT ITAB BY (CRIT). "depending on CRIT content i.e 'VKBUR' , 'BUDAT' it will sort using this field

Regards

Marcin

hi,

i want to sort the int table with variable parameters, like VKBUR, BUDAT, VKORG, MATNR, etc.

for that ive created a char variable of 5 length (say CRIT) and assigned value based on selection criteria by the user.

Then, im using SORT ITAB BY CRIT. Its not getting sorted properly.

Any suggestions,

Regards,

Naveen

5 REPLIES 5
Read only

Former Member
0 Likes
638

please send the code with internal table structure, so ican send th correct code what to write..

Regards,

Prabhudas

Read only

Former Member
0 Likes
638

Probably your assignement to variable could be wrong CRIT.

Check in debugger if the selected field is getting passed correctly.

Regards

Shital

Read only

Former Member
0 Likes
638

Hi,

Char data type sort frist charectar wsie.

if u sort itab value -- 20

1

2

11

3

29.

After Sort Output:- 1

11

2

20

29

3

I sugest define CRIT - NUMC type.

Read only

MarcinPciak
Active Contributor
0 Likes
639

Field is provided dynamically so it must be enclosed in paranthesis.


SORT ITAB BY (CRIT). "depending on CRIT content i.e 'VKBUR' , 'BUDAT' it will sort using this field

Regards

Marcin

Read only

Former Member
0 Likes
638

You can easily achieve it by using filed symbols.

Assign the field (upon which you require sorting) to field symbol<Fs> and then sort the table with (<Fs>)reference.

Sample code is given below for your help.

DATA: BEGIN OF x1 OCCURS 0,
        t1 TYPE string,
        t2 TYPE string,
        t3 TYPE string,
       END OF x1.

FIELD-SYMBOLS: <fs> TYPE ANY.

x1-t1 = 'B'.
x1-t2 = 'D'.
x1-t3 = '9'.
APPEND x1.

x1-t1 = 'C'.
x1-t2 = 'X'.
x1-t3 = '4'.
APPEND x1.

x1-t1 = 'F'.
x1-t2 = 'Y'.
x1-t3 = '6'.
APPEND x1.

LOOP AT x1.
  WRITE: / x1-t1, '    ', x1-t2, '    ', x1-t3.
ENDLOOP.

SKIP 2.

* Sort by field T1
ASSIGN 't1' TO <fs>.
SORT x1 BY (<fs>).

LOOP AT x1.
  WRITE: / x1-t1, '    ', x1-t2, '    ', x1-t3.
ENDLOOP.

skip 2.

* Sort by field T3
ASSIGN 't3' TO <fs>.
SORT x1 BY (<fs>).

LOOP AT x1.
  WRITE: / x1-t1, '    ', x1-t2, '    ', x1-t3.
ENDLOOP.