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 Operation on Date and Time fields

Former Member
0 Likes
12,730

Dear All,

I am trying to SORT the data in descending order based on date and time. System is behaving weird and I am not able to get the actual results. Please suggest me regarding SORT operation on date and time fields.

Note: In my actual scenario my internal table has many fields along with date and time fields.

Code:

TYPES: BEGIN OF temp,

lv_date TYPE d,

lv_time TYPE t,

END OF temp.

DATA: itab TYPE TABLE OF temp,

wa TYPE temp.

wa-lv_date = '20101218'. " yyyymmdd

wa-lv_time = '050505'. " hhmmss

APPEND wa TO itab.

wa-lv_date = '20101219'.

wa-lv_time = '050504'.

APPEND wa TO itab.

wa-lv_date = '20101218'.

wa-lv_time = '050506'.

APPEND wa TO itab.

SORT itab STABLE BY lv_date lv_time DESCENDING.

LOOP AT itab INTO wa.

write:/ wa-lv_date, lv_time.

ENDLOOP.

Result: Date Time

20101218 050506

20101218 050505

20101219 050504

Expected Result:

Date Time

20101219 050504

20101218 050506

20101218 050505

Thanks in Advance.

Regards,

Somu.

4 REPLIES 4
Read only

Former Member
0 Likes
4,183

Hi,

Try this

SORT itab STABLE BY lv_date DESCENDING

lv_time DESCENDING.

Regards,

Madhukar Shetty

Read only

Former Member
0 Likes
4,183

Thanks for the solution. Jai ho SDN and the users.

Read only

Former Member
0 Likes
4,183

hai friend,

TYPES: BEGIN OF temp,

lv_date TYPE d,

lv_time TYPE t,

END OF temp.

DATA: itab TYPE TABLE OF temp,

wa TYPE temp.

wa-lv_date = '20101218'. " yyyymmdd

wa-lv_time = '050505'. " hhmmss

APPEND wa TO itab.

wa-lv_date = '20101219'.

wa-lv_time = '050504'.

APPEND wa TO itab.

wa-lv_date = '20101218'.

wa-lv_time = '050506'.

APPEND wa TO itab.

SORT itab by lv_time lv_date DESCENDING.

LOOP AT itab INTO wa.

write:/ wa-lv_date, wa-lv_time.

ENDLOOP.

do like this u can get the out put i checked also.

thanks ,

anji.

Read only

Former Member
4,183

Hi Soma,

The system is behaving correctly. How ? Let me explain....

When you write SORT ITAB component1 ASCENDING / DESCENDING , then the component1 of itab will be sorted in ascending or descending manner.

And here if you dont mention ASCENDING / DESCENDING, then by default it is sorted in the Ascending mode.

But in your case you have coded like this -:

SORT itab STABLE BY lv_date lv_time DESCENDING.

This means first itab will be sorted in Ascending mode for lv_date (from lower date to higher date) and then within the same date range, the table will be sorted in the descending mode for lv_time (From higher time to lower time).

But if you code like this -

SORT itab STABLE BY lv_date DESCENDING lv_time DESCENDING.

then both lv_date and lv_time will be sorted in descending mode.

Thanks

Deb