Skip to Content
author's profile photo Former Member
Former Member

sorting in internal table.

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jan 23, 2005 at 04:11 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 23, 2005 at 06:19 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      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.

  • Posted on Jan 22, 2005 at 05:25 PM

    could it be that you were trying to remove duplicates?

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

    Christian

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 23, 2005 at 03:01 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 24, 2005 at 08:32 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 24, 2005 at 08:33 AM

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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.