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

Former Member
0 Likes
728

I want to sort the records.ie.to make loop inside loop.

for eg

Take first record from loop, compare 2,3,4,5,6th record in loop.

if record matches swap it or add the current record with matching record and save in internaltable.

how to do this..

X Y

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000002 HWZK000003

HWZK000002 HWZK000003

HWZK000005 HWZK000004

HWZK000005 HWZK000004

HWZK000003 HWZK000005

HWZK000003 HWZK000005

it should be comparing Each "Y" value should

be compared with "X" value and do sorted as below.

SORT command is not usefull here i guess.

ambichan.

7 REPLIES 7
Read only

ChristianFi
Active Participant
0 Likes
693

could it be that you were trying to remove duplicates?

Have a look on the "delete adjacent duplicates" statement.

Christian

Read only

Former Member
0 Likes
693

hai,

its not deleting case.

I want above output sorted in this below order.

HWZK000004        HWZK000002

HWZK000004         HWZK000002

HWZK000005        HWZK000004

HWZK000005         HWZK000004

HWZK000003        HWZK000005

HWZK000003         HWZK000005

HWZK000002        HWZK000003

HWZK000002        HWZK000003

i think if i know how to use loop inside the loop

,compare and then put the required record in seperated

internal table. i can solve this problem.

but i dont know coding loopp inside loop is good or not.

ambichan.

Read only

Former Member
0 Likes
693

Hi Ambi,

First please clarify this, you said when records match you want to add the current and matching record and save it in the internal table - some more explanations.

Can you also brief about the comparison you are doing with X and Y.

DATA: BEGIN OF intab,

str(10) TYPE c,

END OF intab.

DATA: i_tab LIKE TABLE OF intab WITH HEADER LINE.

i_tab-str = 'HWZK000005'.

APPEND i_tab.

i_tab-str = 'HWZK000005'.

APPEND i_tab.

i_tab-str = 'HWZK000004'.

APPEND i_tab.

i_tab-str = 'HWZK000004'.

APPEND i_tab.

i_tab-str = 'HWZK000002'.

APPEND i_tab.

i_tab-str = 'HWZK000002'.

APPEND i_tab.

LOOP AT i_tab.

WRITE:/ i_tab-str.

ENDLOOP.

SORT i_tab BY str.

LOOP AT i_tab.

WRITE:/ i_tab-str.

ENDLOOP.

The above code will give sorted output.

Thanks and Regards,

Kathir

Read only

Former Member
0 Likes
693

hey,

Actually let me tell u my requirment.

I have IMPORT Transport Request number in X column, and

Pretransport number in Y column as below .

note: filter of table record list is displayed below

X-Transport Req no Y-Pre Req Transport no,

HWZK000001 HWZK000000

HWZK000001 HWZK000000

HWZK000006 HWZK000003

HWZK000006 HWZK000003

HWZK000006 HWZK000003

HWZK000003 HWZK000005

HWZK000003 HWZK000005

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000004 HWZK000002

HWZK000005 HWZK000004

HWZK000005 HWZK000004

HWZK000005 HWZK000004

HWZK000005 HWZK000004

HWZK000005 HWZK000004

HWZK000005 HWZK000004

I want to import this Tranposrt&Pre req transport number one by one

thru the FM.

While Import the above Transport Req no, rule is

"Y-Pre req transport no" should be first imported

then only "X-Transport Req no" will be imported

thru FM.

But problem here is. if "Y-pre Req Transport no" already

exists in "X-Transport Req No" column later then current

"Y-pre Req Transport no" cant be imported.

Every Y column pretransport no should be imported before is the

main logic here.

so what i have planned is lets first make continuity

of these nos as X should continue with Y, and Y- should continue with X values

continuosulty till this loop get end.

So that we can easily import as per the sequece no.

My output of import sequence should be as follows.

HWZK000001           KANRI0001            HWZK000000

HWZK000001           KANRI0001            HWZK000000

HWZK000004           KANRI0001            HWZK000002

HWZK000004           KANRI0001            HWZK000002

HWZK000004           KANRI0001            HWZK000002

HWZK000004           KANRI0001            HWZK000002

HWZK000004           KANRI0001            HWZK000002

HWZK000004           KANRI0001            HWZK000002

HWZK000005           KANRI0001            HWZK000004

HWZK000005           KANRI0001            HWZK000004

HWZK000005           KANRI0001            HWZK000004

HWZK000005           KANRI0001            HWZK000004

HWZK000005           KANRI0001            HWZK000004

HWZK000005           KANRI0001            HWZK000004

HWZK000003           KANRI0001            HWZK000005

HWZK000003           KANRI0001            HWZK000005

HWZK000006           KANRI0001            HWZK000003

HWZK000006           KANRI0001            HWZK000003

HWZK000006           KANRI0001            HWZK000003

if u need more explanation pls let me know.

I dont know how to sort this in loop technically.

ambichan.

Read only

0 Likes
693

I think I understand your requirement. Not sure why you have duplicates in your data, for example, why does HWZK000001 HWZK000000 appear twice?

I will describe an algorithm that I think meets your needs but I will assume no duplicates and that there are no circular references, i.e 5 must come before 4 and 4 must come before 5. If you must handle duplicates, the algorithm will need to be adjusted.

You will need a second internal table. I will refer to your original table as ORIG and the new table as ORDER. ORIG has fields X and Y, and ORDER only has field X.

LOOP at ORIG.

..READ table ORDER looking for ORDER-X = ORIG-X. Save sy-index as INDEX_1.

..IF found

….READ table ORDER looking for ORDER-X = ORIG-Y. Save sy-index as INDEX_2.

….IF found

……IF INDEX_1 LT INDEX_2 then error

……ELSE OK as is

….ELSE not found

……INSERT ORIG into ORDER at location INDEX_1 – 1, i.e. just before

….ENDIF

..ELSE not found

….READ table ORDER looking for ORDER-X = ORIG-Y. Save sy-index as INDEX_3.

….IF found

……INSERT ORIG into ORDER at location INDEX_3 + 1, i.e. just after

….ELSE not found

……INSERT ORIG with ORIG-X = ORDER-Y at end

……INSERT ORIG with ORIG-X = ORDER-X at end

….ENDIF

..ENDIF

ENDLOOP.

ORDER is now the order of your transports.

END.

I ran a few desk checks thru this algorithm and I think it should work.

Let us know how it goes.

Read only

Former Member
0 Likes
693

charles,

i solved this problem in different logic.

ur logic seems to be good choice..but no time to replace it.Thanks for your timely help.

ambichan

Read only

Former Member
0 Likes
693

i have rewarded points..if not saved pls let me know