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
928

Hi,

During sorting the internal table i was facing the problem...

See internal table having two fields namely num and name..

sort itab descending by num-->It works...

But

sort itab descending by ____.Here i need to display the value in run time..How it is possible.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
900

Use Syntax

SORT itab by (fieldname).

Regards

7 REPLIES 7
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
900

Hi,

If the fields are not many try using CASE ENDCASE.

CASE FLD.

WHEN FLD1.

SORT ITAB BY FLD1.

WHEN FLD2.

SORT ITAB BY FLD2.

ENDCASE.

Regards,

Sesh

Read only

Former Member
0 Likes
900

You can't SORT an internal table during runtime, because you need to mention the SORT field in the SORT statement in the coding part.

Regards,

Pavan

Read only

RaymondGiuseppi
Active Contributor
0 Likes
901

Use Syntax

SORT itab by (fieldname).

Regards

Read only

former_member491305
Active Contributor
0 Likes
900

Hi ,

Pass it within the brackets.

Data var1(35).

If condition1 = true.

var1 = 'FIELD1'.

else.

var1 = 'FIELD2'.

Endif.

Sort itab by ( var1 ).

Read only

Former Member
0 Likes
900

Instead you can develop a code of this format....

parameters: p_string(10) type c default 'CARRID'.

DATA: BEGIN OF fs_spfli,

carrid TYPE spfli-carrid,

connid TYPE spfli-connid,

countryfr TYPE spfli-countryfr,

END OF fs_spfli.

DATA: t_spfli LIKE STANDARD TABLE OF fs_spfli.

START-OF-SELECTION.

SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE t_spfli.

IF P_STRING EQ 'CARRID'.

sort t_spfli by CARRID.

ELSEIF P_STRING EQ 'CONNID'.

SORT T_SPFLI BY CONNID.

ENDIF.

loop at t_spfli into fs_spfli.

write:

fs_spfli-carrid.

write fs_spfli-connid.

endloop.

Regards,

Pavan

Read only

Former Member
0 Likes
900

use token method:

data: fname type string.

during runtime :

fname = 'NUM' or

fname = 'NAME'.

SORT itab DESCENDING BY (fname).

Reward if useful

Regards

Pradeep

Read only

Former Member
0 Likes
900

You can use following method :

SORT <itab> BY (otab).

where otab is an internal table of type abap_sortorder_tab.

declare it as:

DATA: otab TYPE abap_sortorder_tab,

oline TYPE abap_sortorder.

in ur code:

oline-name = 'NUM'.

oline-descending = 'X'. " Descending

append oline to otab.

clear oline.

oline-name = 'NAME'.

oline-descending = ' '. " AScending

append oline to otab.

Reward if helpful

Regards

Prax