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

How to Sort Ascending by Multiple Fields?

Former Member
0 Likes
30,522

Hi Guys,

Just want to ask something, I know its weird but Im having a problem with the sorting of internal tables with multiple selections or fields.Please see my code below:

SORT table BY  fieldname   ASCENDING

                        dates          ASCENDING

                        times          ASCENDING

                        begda         ASCENDING

                        process_id  ASCENDING.

There are 2 records. All fields have the same record except for process_id:

Fieldname    date            times      begda           process_id

field1          20140201    121222    20140201      000008

field1          20140201    121222    20140201      000005

I dont believe I should sort the process_id by descending right?

Please help!

KR,

DF

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
11,030

Hi,

Heres an extra info regarding my query above as asked by Gupta:

I will be adding an additional data in the table.

Fieldname    date            times      begda           process_id

field1          20140201    121222    20140201      000008

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005


My expected output is:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005

field1          20140201    121222    20140201      000008


However, after it passes to code SORT,


SORT table BY  fieldname   ASCENDING

                        dates          ASCENDING

                        times          ASCENDING

                        begda         ASCENDING

                        process_id  ASCENDING.


it becomes like this:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000008

field1          20140201    121222    20140201      000005


The last two records are not properly sorted out. Need your help here experts

Hi Guys,

Just want to ask something, I know its weird but Im having a problem with the sorting of internal tables with multiple selections or fields.Please see my code below:

SORT table BY  fieldname   ASCENDING

                        dates          ASCENDING

                        times          ASCENDING

                        begda         ASCENDING

                        process_id  ASCENDING.

There are 2 records. All fields have the same record except for process_id:

Fieldname    date            times      begda           process_id

field1          20140201    121222    20140201      000008

field1          20140201    121222    20140201      000005

I dont believe I should sort the process_id by descending right?

Please help!

KR,

DF

16 REPLIES 16
Read only

jogeswararao_kavala
Active Contributor
0 Likes
11,030

If the issue is with process_id value then, try with

SORT table BY fieldname process_id  ASCENDING.


Behavior of SORT is seen like you've mentioned when Date, time fields are involved.


Regards

KJogeswaraRao

Read only

0 Likes
11,030

Hi Jogeswara,

Yes that eould do, how ever, it will not work since the process_id will only increment if the date and time stamp(fields dates and times) are both the same. the table I am actually referring is the DCTLog. I cannot make it be sorted the way it should have been sorted out

Read only

0 Likes
11,030

OK I understand.

Then you should try different sort combinations for date/time fields only the first being  DESCENDING to Time field , like

SORT table BY  fieldname   ASCENDING

                        dates          ASCENDING

                        times          DESCENDING

                        begda         ASCENDING

                        process_id  ASCENDING.


Also try by combining dates, times fields to a timestamp field and sort like your original syntax.

Read only

0 Likes
11,030

Hi Jogeswara,

Ill try but i dont think that would work. Thanks for the suggestions tho.

Read only

Former Member
0 Likes
11,030

Hi Guys,

Thread not yet answered. I need help please

KR,

DF

Read only

VenkatRamesh_V
Active Contributor
0 Likes
11,030

Hi,

Try,

SORT table BY  fieldname   ASCENDING    

                          begda         ASCENDING              

                           process_id  ASCENDING

                          dates           ASCENDING

                          times           ASCENDING.

Hope it helpful,

Regards,

Venkat.V

Read only

0 Likes
11,030

Hi Venkat,

That wont work since the process_id is only being incremented if the time and date stamp is the same.

KR,

DF

Read only

0 Likes
11,030

Hi,

Try it.

Hope it helpful,

Regards,

Venkat.V

Read only

Former Member
0 Likes
11,030

Hi,

Can you tell me what O/P you are expecting from the same itab-data after sorting.


Regards,


Gupta

Read only

0 Likes
11,030

Hi Gupta,

I will be adding an additional data in the table.

Fieldname    date            times      begda           process_id

field1          20140201    121222    20140201      000008

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005


My expected output is:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005

field1          20140201    121222    20140201      000008


However, after it passes to code SORT, it becomes like this:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000008

field1          20140201    121222    20140201      000005


Do you know how to properly correct the sorting of this one?


KR,

Philip


Read only

Former Member
0 Likes
11,031

Hi,

Heres an extra info regarding my query above as asked by Gupta:

I will be adding an additional data in the table.

Fieldname    date            times      begda           process_id

field1          20140201    121222    20140201      000008

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005


My expected output is:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000005

field1          20140201    121222    20140201      000008


However, after it passes to code SORT,


SORT table BY  fieldname   ASCENDING

                        dates          ASCENDING

                        times          ASCENDING

                        begda         ASCENDING

                        process_id  ASCENDING.


it becomes like this:


Fieldname    date            times      begda           process_id

field1          20140101    121222    20140101      000000

field1          20140201    111524    20140201      000003

field1          20140201    121222    20140201      000008

field1          20140201    121222    20140201      000005


The last two records are not properly sorted out. Need your help here experts

Read only

0 Likes
11,030

Hi,

I simulate the issue and do find the solution as below.

TYPES: BEGIN OF ty_data,

  name TYPE string,

  date1 TYPE dats,

  tim1 TYPE tims,

  date2 TYPE dats,

  id TYPE char10,

END OF ty_data.

DATA lt_itab TYPE TABLE OF TY_DATA.

lt_itab = VALUE #(

                  ( name = 'A' DATE1 = '20140201' TIM1 = '121222' DATE2 = '20140201'  ID = '000008' )

                  ( name = 'A' DATE1 = '20140101' TIM1 = '121222'  DATE2 = '20140101' ID = '000000' )

                  ( name = 'A' DATE1 = '20140201' TIM1 = '111524'  DATE2 = '20140201' ID = '000003' )

                  ( name = 'A' DATE1 = '20140201' TIM1 = '121222'  DATE2 = '20140201' ID = '000005' )

).

SORT LT_ITAB BY NAME DATE1 DATE2 ID ASCENDING TIM1 DESCENDING.

**The outcome of the itab after sorting is :-

A2014010112122220140101000000
A2014020111152420140201000003
A2014020112122220140201000005
A2014020112122220140201000008

Hope this is what was expected.

Thanks,

Gupta

Read only

0 Likes
11,030

This message was moderated.

Read only

matt
Active Contributor
0 Likes
11,030

Or using pre 7.4 code, I also get the same results.

This is the program I wrote to try to replicate your problem:

TYPES: BEGIN OF test_ty,

          fieldname TYPE fieldname,

          dates     TYPE d,

          times     TYPE t,

          begda     TYPE begda,

          process_id TYPE c LENGTH 6,

        END OF test_ty.

DATA test_records TYPE STANDARD TABLE OF test_ty.

DATA test_record TYPE test_ty.

test_record-fieldname  = 'field1'.

test_record-dates      = '20140201'.

test_record-times      = '121222'.

test_record-begda      = '20140201'.

test_record-process_id = '000008'.

INSERT test_record INTO TABLE test_records.

test_record-fieldname  = 'field1'.

test_record-dates      = '20140101'.

test_record-times      = '121222'.

test_record-begda      = '20140101'.

test_record-process_id = '000000'.

INSERT test_record INTO TABLE test_records.

test_record-fieldname  = 'field1'.

test_record-dates      = '20140201'.

test_record-times      = '111524'.

test_record-begda      = '20140201'.

test_record-process_id = '000003'.

INSERT test_record INTO TABLE test_records.

test_record-fieldname  = 'field1'.

test_record-dates      = '20140201'.

test_record-times      = '121222'.

test_record-begda      = '20140201'.

test_record-process_id = '000005'.

INSERT test_record INTO TABLE test_records.

LOOP AT test_records INTO test_record.

   WRITE:/ test_record-fieldname, test_record-dates DD/MM/YYYY,

           test_record-times, test_record-begda, test_record-process_id.

ENDLOOP.

SKIP.

SORT test_records BY fieldname   ASCENDING

                      dates       ASCENDING

                      times       ASCENDING

                      begda       ASCENDING

                      process_id  ASCENDING.

LOOP AT test_records INTO test_record.

   WRITE:/ test_record-fieldname, test_record-dates DD/MM/YYYY,

           test_record-times, test_record-begda, test_record-process_id.

ENDLOOP.

When I run it I also get, as expected,

field1      01.01.2014 121222 01.01.2014 000000

field1      01.02.2014 111524 01.02.2014 000003

field1      01.02.2014 121222 01.02.2014 000005

field1      01.02.2014 121222 01.02.2014 000008

What types are you using for each of the fields in your internal table? What type is your internal table?

Read only

Former Member
0 Likes
11,030

What is the domain of process id? is there a conversion exit for it?

Rob

Read only

Former Member
0 Likes
11,030

Maybe it's due to the the internal format of the fields? I would say the table does not have "exactly" what you posted but i may be wrong.. Check for the fields internal formats and/or domains.

Best regards