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

ALV Sort by time

Hi Experts!

I have a problem in sorting.

I have a field called Remaining time having data type char20.

Format is HH:MM:SS.

The problem is, i want it to be sorted in a way that a number is sorted.

Example (descending):

I want it to be sorted this way:

121:89

111:00

12:34

10:78

00:00

-01:44

-10:22

-11:45

-109:11

but in my program, it is sorted as text:

121:89

12:34

111:00

10:78

-109:11

-11:45

-10:22

-01:44

00:00

Edited by: navi on Aug 11, 2008 4:04 PM

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Aug 11, 2008 at 08:06 AM

    Hi,

    Step 1. Sort your internal table by time field before passing it to

    ALV.

    Step 2. Pass the sorted table.

    OR

    use function module reuse_alv_grid_display.

    IMPORTING

    REFERENCE(I_INTERFACE_CHECK) DEFAULT SPACE

    VALUE(I_BYPASSING_BUFFER) TYPE CHAR01 DEFAULT SPACE

    VALUE(I_BUFFER_ACTIVE) DEFAULT SPACE

    REFERENCE(I_CALLBACK_PROGRAM) LIKE SY-REPID DEFAULT SPACE

    REFERENCE(I_CALLBACK_PF_STATUS_SET) TYPE SLIS_FORMNAME DEFAULT

    SPACE

    REFERENCE(I_CALLBACK_USER_COMMAND) TYPE SLIS_FORMNAME DEFAULT

    SPACE

    REFERENCE(I_CALLBACK_TOP_OF_PAGE) TYPE SLIS_FORMNAME DEFAULT

    SPACE

    REFERENCE(I_CALLBACK_HTML_TOP_OF_PAGE) TYPE SLIS_FORMNAME

    DEFAULT SPACE

    REFERENCE(I_CALLBACK_HTML_END_OF_LIST) TYPE SLIS_FORMNAME

    DEFAULT SPACE

    REFERENCE(I_STRUCTURE_NAME) LIKE DD02L-TABNAME OPTIONAL

    REFERENCE(I_BACKGROUND_ID) TYPE SDYDO_KEY DEFAULT SPACE

    REFERENCE(I_GRID_TITLE) TYPE LVC_TITLE OPTIONAL

    REFERENCE(I_GRID_SETTINGS) TYPE LVC_S_GLAY OPTIONAL

    REFERENCE(IS_LAYOUT) TYPE SLIS_LAYOUT_ALV OPTIONAL

    REFERENCE(IT_FIELDCAT) TYPE SLIS_T_FIELDCAT_ALV OPTIONAL

    REFERENCE(IT_EXCLUDING) TYPE SLIS_T_EXTAB OPTIONAL

    REFERENCE(IT_SPECIAL_GROUPS) TYPE SLIS_T_SP_GROUP_ALV OPTIONAL

    REFERENCE(IT_SORT) TYPE SLIS_T_SORTINFO_ALV OPTIONAL ---> uncomment this (see details bellow)

    REFERENCE(IT_FILTER) TYPE SLIS_T_FILTER_ALV OPTIONAL

    REFERENCE(IS_SEL_HIDE) TYPE SLIS_SEL_HIDE_ALV OPTIONAL

    REFERENCE(I_DEFAULT) DEFAULT 'X'

    REFERENCE(I_SAVE) DEFAULT SPACE

    REFERENCE(IS_VARIANT) LIKE DISVARIANT STRUCTURE DISVARIANT

    OPTIONAL

    REFERENCE(IT_EVENTS) TYPE SLIS_T_EVENT OPTIONAL

    REFERENCE(IT_EVENT_EXIT) TYPE SLIS_T_EVENT_EXIT OPTIONAL

    REFERENCE(IS_PRINT) TYPE SLIS_PRINT_ALV OPTIONAL

    REFERENCE(IS_REPREP_ID) TYPE SLIS_REPREP_ID OPTIONAL

    REFERENCE(I_SCREEN_START_COLUMN) DEFAULT 0

    REFERENCE(I_SCREEN_START_LINE) DEFAULT 0

    REFERENCE(I_SCREEN_END_COLUMN) DEFAULT 0

    REFERENCE(I_SCREEN_END_LINE) DEFAULT 0

    REFERENCE(I_HTML_HEIGHT_TOP) TYPE I DEFAULT 0

    REFERENCE(I_HTML_HEIGHT_END) TYPE I DEFAULT 0

    REFERENCE(IT_ALV_GRAPHICS) TYPE DTC_T_TC OPTIONAL

    REFERENCE(IT_HYPERLINK) TYPE LVC_T_HYPE OPTIONAL

    REFERENCE(IT_ADD_FIELDCAT) TYPE SLIS_T_ADD_FIELDCAT OPTIONAL

    REFERENCE(IT_EXCEPT_QINFO) TYPE SLIS_T_QINFO_ALV OPTIONAL

    REFERENCE(IR_SALV_FULLSCREEN_ADAPTER) TYPE REF TO

    CL_SALV_FULLSCREEN_ADAPTER OPTIONAL

    EXPORTING

    REFERENCE(E_EXIT_CAUSED_BY_CALLER)

    REFERENCE(ES_EXIT_CAUSED_BY_USER) TYPE SLIS_EXIT_BY_USER

    TABLES

    T_OUTTAB

    EXCEPTIONS

    PROGRAM_ERROR

    ( Using internal table IT_SORT, the caller determines the sort order and/or the subtotalling of the basic list.)

    Regards,

    anirban

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 11, 2008 at 08:12 AM

    hi

    what is the field for your time , just use this statement to sort it (sort internal table name by field name)

    (like sort itab by time.)

    thnaks

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi all!

      Yes, I already tried sorting it in the program itself and in the ALV display.

      But the display is wrong since it sorted the time as a text.

      I want it it to be displayed/sorted in a way a number is sorted.

      How can i do that?

      Is there an event that can solve it? or a data type perhaps that sorts a character type fields as number?

      Pls help.

      Thanks!

  • author's profile photo Former Member
    Former Member
    Posted on Aug 11, 2008 at 08:23 AM

    Hi

    Solution 1: Change the data type of the field from char to type T (time).

    Solution 2: Add 2 new fields in the internal table (Say X(4) and Y(4) of data type N) and write the following code.

    LOOP AT ITB.

    SPLIT ITAB-REMTIME BY ':' INTO ITAB-X ITAB-Y. " Remaining time Field into X and Y

    ENDLOOP.

    SORT ITAB BY X Y.

    Hope it helps.

    Murthy

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Kalyanam,

      But the sorting will be done by the sorting functionality of ALV.

      Not in the program.

      How will I do that in such a way that ALV sort functionality, will sort my data as numbers?

      Thanks.

  • Posted on Aug 11, 2008 at 08:37 AM

    Hi Navi,

    As you have stated the problem is caused by the data type for the field being sorted.

    The problem as I see it is you want to display the time as HHH:MM:SS which is different from the internal represtation of time in ABAP as HHMMSS for a 24 hour clock.

    My Advice to you is to split the duration into 3 separate fields HHH MM SS and to sort by these either in the ALV or before.

    Also trap when the sort function is called so that you can test if it is your displayed field being sorted and act accordingly. The fields used for sorting do not have to be displayed, but they do need to be part of the fieldcat definition.

    Kind Regards

    David Cooper

    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.