‎2010 Dec 07 5:05 AM
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.
‎2010 Dec 07 5:08 AM
Hi,
Try this
SORT itab STABLE BY lv_date DESCENDING
lv_time DESCENDING.
Regards,
Madhukar Shetty
‎2010 Dec 07 5:24 AM
‎2010 Dec 07 5:26 AM
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.
‎2010 Dec 07 5:30 AM
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