<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Programming riddle: Sort rows via drag and drop in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611542#M25229</link>
    <description>&lt;P&gt;Thanks for the hint! The sorting itself is not the problem. Knowing or calculating the new sort order is the challenge.&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jan 2018 17:12:29 GMT</pubDate>
    <dc:creator>EnnoWulff</dc:creator>
    <dc:date>2018-01-25T17:12:29Z</dc:date>
    <item>
      <title>Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611540#M25227</link>
      <description>&lt;P&gt;Hi there!&lt;/P&gt;
  &lt;P&gt;today I doubt that I do programming for so many years...&lt;/P&gt;
  &lt;P&gt;The task: Provide an easy sorting mechanism in ALV grid.&lt;/P&gt;
  &lt;P&gt;The user should be able to mark any lines (one, a block of lines or arbitrary lines) and move them via drag and drop to another place.&lt;/P&gt;
  &lt;P&gt;I want to have a solution that can be used instead of the cl_gui_alv_grid. Therefore I inherited the class and implemented drag and drop feature.&lt;/P&gt;
  &lt;P&gt;The task seems to be quite easy: renumber the marked rows, find the new position in the data table and renumber the old entries.&lt;/P&gt;
  &lt;P&gt;May be I do think to complex or it really is not that trivial...&lt;/P&gt;
  &lt;P&gt;I would like to invite you to a challenge and try to find a working solution.&lt;/P&gt;
  &lt;P&gt;I myself had three approaches which either do not work for every selection or which use too much memory and performance.&lt;/P&gt;
  &lt;P&gt;The following code provides a demo report that&lt;/P&gt;
  &lt;UL&gt;
   &lt;LI&gt;displays table T005T (countries) in alv grid&lt;/LI&gt;
   &lt;LI&gt;has a derived cl_gui_alv_grid class&lt;/LI&gt;
   &lt;LI&gt;has a dragdrop-data-object class&lt;/LI&gt;
   &lt;LI&gt;is prepared for dragging and dropping lines (or a single cell)&lt;/LI&gt;
  &lt;/UL&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106342-ddsort1.png" /&gt;&lt;/P&gt;
  &lt;P&gt;Just copy and paste the following code and try to find a simple solution that just sets the new order-id in the given field.&lt;/P&gt;
  &lt;P&gt;I am excited to see your solutions! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
  &lt;P&gt;Enno&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;EXAMPLE&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;this is one of the most complex example: marking several lines that are not in one block and dragging them to another line:&lt;/P&gt;
  &lt;P&gt;Lines 4,6,7 and 9 are marked and are moved to line 2.&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106343-ddsort2.png" /&gt;&lt;/P&gt;
  &lt;P&gt;After dropping the lines the renumbering will be done and shown in field NATIO:&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106344-ddsort3.png" /&gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;HINTS&lt;/STRONG&gt;&lt;/P&gt;
  &lt;P&gt;It matters if you move the lines up or down.&lt;/P&gt;
  &lt;P&gt;Calculating the new order number should be preferred. I have a solution where I copy the selected lines to a temporary created identical table, deleting the marked lines, do the numbering and insertion at new position. This might be a problem in huge tables...&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;CODE&lt;/STRONG&gt;&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;REPORT .

CLASS lcl_dragdrop_sort DEFINITION.
  PUBLIC SECTION.
    DATA at_rows TYPE lvc_t_row.
    DATA ao_data TYPE REF TO data.
ENDCLASS.

CLASS lcl_gui_alv_grid_sort DEFINITION
  INHERITING FROM cl_gui_alv_grid.
  PUBLIC SECTION.
    METHODS set_sort_field
      IMPORTING
        fieldname TYPE clike.
  PROTECTED SECTION.
    DATA mv_sort_field TYPE char30 .
    CONSTANTS mc_sort_flavor TYPE c LENGTH 10 VALUE 'SortMe'.
    METHODS on_drag            FOR EVENT ondrag       OF cl_gui_alv_grid
      IMPORTING e_column e_row e_dragdropobj es_row_no sender.
    METHODS on_drop            FOR EVENT ondrop       OF cl_gui_alv_grid
      IMPORTING e_column e_row e_dragdropobj es_row_no sender.
ENDCLASS.

CLASS lcl_gui_alv_grid_sort IMPLEMENTATION.
  METHOD on_drag.
    DATA lo_dragdrop_sort TYPE REF TO lcl_dragdrop_sort.
    lo_dragdrop_sort = NEW #( ).
    sender-&amp;gt;get_selected_rows( IMPORTING et_index_rows = lo_dragdrop_sort-&amp;gt;at_rows ).

    IF lo_dragdrop_sort-&amp;gt;at_rows IS INITIAL.
     "No lines selected: get current cell
      sender-&amp;gt;get_current_cell(
        IMPORTING
          e_row = DATA(lv_row) ).
      IF NOT lv_row IS INITIAL.
        APPEND VALUE #( index = lv_row ) TO lo_dragdrop_sort-&amp;gt;at_rows.
      ENDIF.
    ENDIF.

    lo_dragdrop_sort-&amp;gt;ao_data = mt_outtab.
    e_dragdropobj-&amp;gt;object ?= lo_dragdrop_sort.

  ENDMETHOD.

  METHOD on_drop.
    DATA lo_dragdrop_sort    TYPE REF TO lcl_dragdrop_sort.
    FIELD-SYMBOLS &amp;lt;lft_data&amp;gt; TYPE table.

    CHECK e_dragdropobj-&amp;gt;flavor = mc_sort_flavor.

    TRY.
        lo_dragdrop_sort ?= e_dragdropobj-&amp;gt;object.
        CHECK lo_dragdrop_sort-&amp;gt;at_rows  IS NOT INITIAL.
      CATCH cx_sy_move_cast_error.
        RETURN.
    ENDTRY.

    ASSIGN lo_dragdrop_sort-&amp;gt;ao_data-&amp;gt;* TO &amp;lt;lft_data&amp;gt;.

    LOOP AT &amp;lt;lft_data&amp;gt; ASSIGNING FIELD-SYMBOL(&amp;lt;lfs_data&amp;gt;).
      ASSIGN COMPONENT mv_sort_field OF STRUCTURE &amp;lt;lfs_data&amp;gt; 
          TO FIELD-SYMBOL(&amp;lt;lfv_sortn&amp;gt;).
      CHECK sy-subrc = 0.
      &amp;lt;lfv_sortn&amp;gt; = CONV numc10( sy-tabix ).
      "-------&amp;gt;
      "PLACE YOUR SOLUTION HERE
      "AND HERE
      "...
      "....AND
      "------&amp;gt; HERE
      "---------------------------------------------&amp;gt; SOLUTION!! &amp;lt;------"
    ENDLOOP.
    
    "Set sort field
    me-&amp;gt;get_sort_criteria( IMPORTING et_sort = DATA(lt_sort) ).
    IF NOT line_exists( lt_sort[ fieldname = mv_sort_field ] ).
      INSERT VALUE #( fieldname = mv_sort_field spos = 1 up = abap_true ) 
        INTO lt_sort INDEX 1.
    ENDIF.

   "Set sorting
    me-&amp;gt;set_sort_criteria( lt_sort ).

    "Show changes
    refresh_table_display( ).

  ENDMETHOD.

  METHOD set_sort_field.

    mv_sort_field = fieldname.

    SET HANDLER on_drop    FOR me.
    SET HANDLER on_drag    FOR me.

    DATA(lo_dd) = NEW cl_dragdrop( ).
    lo_dd-&amp;gt;add( dragsrc    = abap_true
                droptarget = abap_true
                flavor     = mc_sort_flavor
                effect     = cl_dragdrop=&amp;gt;copy
                effect_in_ctrl = cl_dragdrop=&amp;gt;move ).

    DATA(lv_dd_handle) = 0.
    lo_dd-&amp;gt;get_handle( IMPORTING handle = lv_dd_handle ).

    "Set drag-drop-handle
    get_frontend_layout( IMPORTING es_layout = DATA(ls_layout) ).
    ls_layout-s_dragdrop-row_ddid = lv_dd_handle.
    set_frontend_layout( ls_layout ).
    refresh_table_display( ).

  ENDMETHOD.
ENDCLASS.

CLASS main DEFINITION.
  PUBLIC SECTION.
    TYPES ty_data       TYPE t005t.
    TYPES ty_data_t     TYPE STANDARD TABLE OF ty_data
                             WITH DEFAULT KEY.
    DATA ms_data        TYPE ty_data.
    DATA mt_data        TYPE ty_data_t.

    DATA mr_grid        TYPE REF TO lcl_gui_alv_grid_sort.
    METHODS start.
  PROTECTED SECTION.
    METHODS selection.
    METHODS display.

ENDCLASS.

CLASS main IMPLEMENTATION.
  METHOD selection.
    SELECT * FROM t005t INTO TABLE mt_data UP TO 10 ROWS 
     WHERE spras = sy-langu.
  ENDMETHOD.

  METHOD start.
    selection( ).
    display( ).
  ENDMETHOD.

  METHOD display.

   WRITE 'DUMMY'.
    
   CREATE OBJECT mr_grid
      EXPORTING
        i_parent      = cl_gui_container=&amp;gt;screen0
        i_appl_events = space.

    DATA lv_structure_name    TYPE dd02l-tabname VALUE 'T005T'.
    DATA ls_variant           TYPE disvariant.
    DATA lv_save              TYPE char01 VALUE 'U'.
    DATA lv_default           TYPE char01 VALUE abap_true.
    DATA ls_layout            TYPE lvc_s_layo.

    ls_layout-sel_mode       = 'A'.
    ls_layout-grid_title     = 'Countries'.

    mr_grid-&amp;gt;set_table_for_first_display(
      EXPORTING
        i_structure_name              = lv_structure_name
        is_variant                    = ls_variant
        i_save                        = lv_save
        i_default                     = lv_default
        is_layout                     = ls_layout
      CHANGING
        it_outtab                     = mt_data ).

    mr_grid-&amp;gt;set_sort_field( 'NATIO' ).

  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  NEW main( )-&amp;gt;start( ).
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jan 2018 16:54:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611540#M25227</guid>
      <dc:creator>EnnoWulff</dc:creator>
      <dc:date>2018-01-25T16:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611541#M25228</link>
      <description>&lt;P&gt;Just a hunch without delving into your problem, can you employ virtual sorting somehow?&lt;/P&gt;
  &lt;P&gt;&lt;A href="https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenvirtual_sort_abexas.htm" target="test_blank"&gt;https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenvirtual_sort_abexas.htm&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;(I know that this was invented by some demand of the ALV framework)&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2018 17:08:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611541#M25228</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2018-01-25T17:08:47Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611542#M25229</link>
      <description>&lt;P&gt;Thanks for the hint! The sorting itself is not the problem. Knowing or calculating the new sort order is the challenge.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2018 17:12:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611542#M25229</guid>
      <dc:creator>EnnoWulff</dc:creator>
      <dc:date>2018-01-25T17:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611543#M25230</link>
      <description>&lt;P&gt;Just found out the following:&lt;/P&gt;
  &lt;P&gt;If ALV grid is in edit-mode then you can move the lines by default even &lt;EM&gt;between &lt;/EM&gt;two lines:&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106345-ddsort4.png" /&gt;&lt;/P&gt;
  &lt;P&gt;but:&lt;/P&gt;
  &lt;P&gt;1. the sorting is wrong when selecting multiple "blocks" of lines &lt;/P&gt;
  &lt;P&gt;2. How can I access the event "dragging complete"? Event DATA_CHANGED does not work for dragging and dropping.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2018 17:36:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611543#M25230</guid>
      <dc:creator>EnnoWulff</dc:creator>
      <dc:date>2018-01-25T17:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611544#M25231</link>
      <description>&lt;P&gt;If the number of lines can be limit to 100/1000/10000, which are Drap&amp;amp;Drop at once:&lt;/P&gt;
  &lt;OL&gt;
   &lt;LI&gt;[main-&amp;gt;selection] Enumerate the data from beginning in steps of 100/1000/10000 like 1100, 1200, 1300&lt;/LI&gt;
   &lt;LI&gt;[lcl_gui_alv_grid_sort-&amp;gt;on_drop] Re-Enumerate dropped lines beginning at insert line like 1301, 1302, 1303&lt;/LI&gt;
   &lt;LI&gt;[lcl_gui_alv_grid_sort-&amp;gt;on_dropcomplete] Use event ondropcomplete to sort and re-enumerate like in step 1&lt;/LI&gt;
  &lt;/OL&gt;
  &lt;P&gt; I put line comment &amp;gt; " new &amp;lt; on changed/new lines.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;REPORT .
CLASS lcl_dragdrop_sort DEFINITION.
  PUBLIC SECTION.
    DATA at_rows TYPE lvc_t_row.
    DATA ao_data TYPE REF TO data.
ENDCLASS.
CLASS lcl_gui_alv_grid_sort DEFINITION
  INHERITING FROM cl_gui_alv_grid.
  PUBLIC SECTION.
    METHODS set_sort_field
      IMPORTING
        fieldname TYPE clike.
  PROTECTED SECTION.
    DATA mv_sort_field TYPE char30 .
    CONSTANTS mc_sort_flavor TYPE c LENGTH 10 VALUE 'SortMe'.
    METHODS on_drag            FOR EVENT ondrag       OF cl_gui_alv_grid
      IMPORTING e_column e_row e_dragdropobj es_row_no sender.
    METHODS on_drop            FOR EVENT ondrop       OF cl_gui_alv_grid
      IMPORTING e_column e_row e_dragdropobj es_row_no sender.
    METHODS on_dropcomplete    FOR EVENT ondropcomplete OF cl_gui_alv_grid " new
      IMPORTING e_row e_column es_row_no e_dragdropobj.                    " new
ENDCLASS.
CLASS lcl_gui_alv_grid_sort IMPLEMENTATION.
  METHOD on_drag.
    DATA lo_dragdrop_sort TYPE REF TO lcl_dragdrop_sort.
    lo_dragdrop_sort = NEW #( ).
    sender-&amp;gt;get_selected_rows( IMPORTING et_index_rows = lo_dragdrop_sort-&amp;gt;at_rows ).
    IF lo_dragdrop_sort-&amp;gt;at_rows IS INITIAL.
      "No lines selected: get current cell
      sender-&amp;gt;get_current_cell(
        IMPORTING
          e_row = DATA(lv_row) ).
      IF NOT lv_row IS INITIAL.
        APPEND VALUE #( index = lv_row ) TO lo_dragdrop_sort-&amp;gt;at_rows.
      ENDIF.
    ENDIF.
    lo_dragdrop_sort-&amp;gt;ao_data = mt_outtab.
    e_dragdropobj-&amp;gt;object ?= lo_dragdrop_sort.
  ENDMETHOD.
  METHOD on_drop.
    DATA lo_dragdrop_sort    TYPE REF TO lcl_dragdrop_sort.
    FIELD-SYMBOLS &amp;lt;lft_data&amp;gt; TYPE table.
    DATA newnatio TYPE i.
    CHECK e_dragdropobj-&amp;gt;flavor = mc_sort_flavor.
    TRY.
        lo_dragdrop_sort ?= e_dragdropobj-&amp;gt;object.
        CHECK lo_dragdrop_sort-&amp;gt;at_rows  IS NOT INITIAL.
        ASSIGN lo_dragdrop_sort-&amp;gt;ao_data-&amp;gt;* TO &amp;lt;lft_data&amp;gt;.
        READ TABLE &amp;lt;lft_data&amp;gt; ASSIGNING FIELD-SYMBOL(&amp;lt;lfs_data&amp;gt;) INDEX e_row-index.
        CHECK sy-subrc EQ 0.
        ASSIGN COMPONENT 'NATIO' OF STRUCTURE &amp;lt;lfs_data&amp;gt; TO FIELD-SYMBOL(&amp;lt;natio&amp;gt;).
        newnatio = &amp;lt;natio&amp;gt;.
        LOOP AT lo_dragdrop_sort-&amp;gt;at_rows ASSIGNING FIELD-SYMBOL(&amp;lt;at_rows&amp;gt;).
          READ TABLE &amp;lt;lft_data&amp;gt; ASSIGNING &amp;lt;lfs_data&amp;gt; INDEX &amp;lt;at_rows&amp;gt;-index.
          CHECK sy-subrc EQ 0.
          ADD 1 TO newnatio.
          ASSIGN COMPONENT 'NATIO' OF STRUCTURE &amp;lt;lfs_data&amp;gt; TO &amp;lt;natio&amp;gt;.
          &amp;lt;natio&amp;gt; = CONV numc10( newnatio ).
        ENDLOOP.
      CATCH cx_sy_move_cast_error.
        RETURN.
    ENDTRY.
*    ASSIGN lo_dragdrop_sort-&amp;gt;ao_data-&amp;gt;* TO &amp;lt;lft_data&amp;gt;.                       " new &amp;gt;&amp;gt;&amp;gt;
*
*    LOOP AT &amp;lt;lft_data&amp;gt; ASSIGNING FIELD-SYMBOL(&amp;lt;lfs_data&amp;gt;).
*      ASSIGN COMPONENT mv_sort_field OF STRUCTURE &amp;lt;lfs_data&amp;gt;
*          TO FIELD-SYMBOL(&amp;lt;lfv_sortn&amp;gt;).
*      CHECK sy-subrc = 0.
*      &amp;lt;lfv_sortn&amp;gt; = CONV numc10( sy-tabix ).
*      "-------&amp;gt;
*      "PLACE YOUR SOLUTION HERE
*      "AND HERE
*      "...
*      "....AND
*      "------&amp;gt; HERE
*      "---------------------------------------------&amp;gt; SOLUTION!! &amp;lt;------"
*    ENDLOOP.
*
*    "Set sort field
*    me-&amp;gt;get_sort_criteria( IMPORTING et_sort = DATA(lt_sort) ).
*    IF NOT line_exists( lt_sort[ fieldname = mv_sort_field ] ).
*      INSERT VALUE #( fieldname = mv_sort_field spos = 1 up = abap_true )
*        INTO lt_sort INDEX 1.
*    ENDIF.
*    "Set sorting
*    me-&amp;gt;set_sort_criteria( lt_sort ).
*
*    "Show changes
*    refresh_table_display( ).                                              " new &amp;lt;&amp;lt;&amp;lt;
  ENDMETHOD.
  METHOD on_dropcomplete.                                                   " new &amp;gt;&amp;gt;&amp;gt;
    DATA lo_dragdrop_sort    TYPE REF TO lcl_dragdrop_sort.
    FIELD-SYMBOLS &amp;lt;lft_data&amp;gt; TYPE table.
    CHECK e_dragdropobj-&amp;gt;flavor = mc_sort_flavor.
    TRY.
        lo_dragdrop_sort ?= e_dragdropobj-&amp;gt;object.
        CHECK lo_dragdrop_sort-&amp;gt;at_rows  IS NOT INITIAL.
      CATCH cx_sy_move_cast_error.
        RETURN.
    ENDTRY.
    ASSIGN lo_dragdrop_sort-&amp;gt;ao_data-&amp;gt;* TO &amp;lt;lft_data&amp;gt;.
    SORT &amp;lt;lft_data&amp;gt; BY ('NATIO').
    LOOP AT &amp;lt;lft_data&amp;gt; ASSIGNING FIELD-SYMBOL(&amp;lt;lfs_data&amp;gt;).
      ASSIGN COMPONENT mv_sort_field OF STRUCTURE &amp;lt;lfs_data&amp;gt;
          TO FIELD-SYMBOL(&amp;lt;lfv_sortn&amp;gt;).
      CHECK sy-subrc = 0.
      &amp;lt;lfv_sortn&amp;gt; = CONV numc10( 1000 + ( sy-tabix * 100 ) ).
      "-------&amp;gt;
      "PLACE YOUR SOLUTION HERE
      "AND HERE
      "...
      "....AND
      "------&amp;gt; HERE
      "---------------------------------------------&amp;gt; SOLUTION!! &amp;lt;------"
    ENDLOOP.
    "Set sort field
    me-&amp;gt;get_sort_criteria( IMPORTING et_sort = DATA(lt_sort) ).
    IF NOT line_exists( lt_sort[ fieldname = mv_sort_field ] ).
      INSERT VALUE #( fieldname = mv_sort_field spos = 1 up = abap_true )
        INTO lt_sort INDEX 1.
    ENDIF.
    "Set sorting
    me-&amp;gt;set_sort_criteria( lt_sort ).
    "Show changes
    refresh_table_display( ).
  ENDMETHOD.                                                               " new &amp;lt;&amp;lt;&amp;lt;
  METHOD set_sort_field.
    mv_sort_field = fieldname.
    SET HANDLER on_drop    FOR me.
    SET HANDLER on_drag    FOR me.
    SET HANDLER on_dropcomplete FOR me.
    DATA(lo_dd) = NEW cl_dragdrop( ).
    lo_dd-&amp;gt;add( dragsrc    = abap_true
                droptarget = abap_true
                flavor     = mc_sort_flavor
                effect     = cl_dragdrop=&amp;gt;copy
                effect_in_ctrl = cl_dragdrop=&amp;gt;move ).
    DATA(lv_dd_handle) = 0.
    lo_dd-&amp;gt;get_handle( IMPORTING handle = lv_dd_handle ).
    "Set drag-drop-handle
    get_frontend_layout( IMPORTING es_layout = DATA(ls_layout) ).
    ls_layout-s_dragdrop-row_ddid = lv_dd_handle.
    set_frontend_layout( ls_layout ).
    refresh_table_display( ).
  ENDMETHOD.
ENDCLASS.
CLASS main DEFINITION.
  PUBLIC SECTION.
    TYPES ty_data       TYPE t005t.
    TYPES ty_data_t     TYPE STANDARD TABLE OF ty_data
                             WITH DEFAULT KEY.
    DATA ms_data        TYPE ty_data.
    DATA mt_data        TYPE ty_data_t.
    DATA mr_grid        TYPE REF TO lcl_gui_alv_grid_sort.
    METHODS start.
  PROTECTED SECTION.
    METHODS selection.
    METHODS display.
ENDCLASS.
CLASS main IMPLEMENTATION.
  METHOD selection.
    SELECT * FROM t005t INTO TABLE mt_data UP TO 10 ROWS
     WHERE spras = sy-langu.
    LOOP AT mt_data ASSIGNING FIELD-SYMBOL(&amp;lt;mt_data&amp;gt;).            " new
      &amp;lt;mt_data&amp;gt;-natio = CONV numc10( 1000 + ( sy-tabix * 100 ) ). " new
    ENDLOOP.                                                      " new
  ENDMETHOD.
  METHOD start.
    selection( ).
    display( ).
  ENDMETHOD.
  METHOD display.
    WRITE 'DUMMY'.
    CREATE OBJECT mr_grid
      EXPORTING
        i_parent      = cl_gui_container=&amp;gt;screen0
        i_appl_events = space.
    DATA lv_structure_name    TYPE dd02l-tabname VALUE 'T005T'.
    DATA ls_variant           TYPE disvariant.
    DATA lv_save              TYPE char01 VALUE 'U'.
    DATA lv_default           TYPE char01 VALUE abap_true.
    DATA ls_layout            TYPE lvc_s_layo.
    ls_layout-sel_mode       = 'A'.
    ls_layout-grid_title     = 'Countries'.
    mr_grid-&amp;gt;set_table_for_first_display(
      EXPORTING
        i_structure_name              = lv_structure_name
        is_variant                    = ls_variant
        i_save                        = lv_save
        i_default                     = lv_default
        is_layout                     = ls_layout
      CHANGING
        it_outtab                     = mt_data ).
    mr_grid-&amp;gt;set_sort_field( 'NATIO' ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  NEW main( )-&amp;gt;start( ).
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jan 2018 19:57:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611544#M25231</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-01-25T19:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611545#M25232</link>
      <description>&lt;P&gt;Thanks for this solution Tibor!&lt;/P&gt;
  &lt;P&gt;This is a simple solution. I remember I also thought a second about this but rejected the solution because in my special case the sort field was just 4 digits long...&lt;/P&gt;
  &lt;P&gt;And as I began to calculate the new positions I thought that it must be possible to exactly calculate it. Maybe it's possible but not that simple...&lt;/P&gt;
  &lt;P&gt;Why did you put your last code in OnDropComplete? In this case it should also work w/o OnDropComplete...?&lt;/P&gt;
  &lt;P&gt;Regards&lt;/P&gt;
  &lt;P&gt;Enno&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 09:35:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611545#M25232</guid>
      <dc:creator>EnnoWulff</dc:creator>
      <dc:date>2018-01-26T09:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611546#M25233</link>
      <description>&lt;P&gt;Sorry, could not post it as reply to comment above. Tried it several times.&lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt;Enno wrote:
Why did you put your last code in OnDropComplete? In this case it should also work w/o OnDropComplete...?&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I used OnDropComplete, because you mentioned it in your additional comment ("2. How can I access the event "dragging complete"?)&lt;/P&gt;
  &lt;P&gt;And it is inspired from paper algorithm.&lt;/P&gt;
  &lt;P&gt;&lt;BR /&gt;Starting with initialized numbering: &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106419-img-5092.jpg" /&gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;On_Drap&lt;/STRONG&gt; move selected lines and number them: &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106420-img-5093.jpg" /&gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;On_Drop&lt;/STRONG&gt; move lines to new position and insert them: &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106421-img-5094.jpg" /&gt;&lt;/P&gt;
  &lt;P&gt;&lt;STRONG&gt;On_DropComplete&lt;/STRONG&gt; initialize numbering for next roundtrip:&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/106422-img-5095.jpg" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 16:06:48 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611546#M25233</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-01-26T16:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: Programming riddle: Sort rows via drag and drop</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611547#M25234</link>
      <description>&lt;P&gt;haha, great! Love your paper work! &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
  &lt;P&gt;The additional question especially referred to the drag-and-drop IN EDIT MODE. In this case the "normal" drag-and-drop events are not triggered.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 16:37:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/programming-riddle-sort-rows-via-drag-and-drop/m-p/611547#M25234</guid>
      <dc:creator>EnnoWulff</dc:creator>
      <dc:date>2018-01-26T16:37:39Z</dc:date>
    </item>
  </channel>
</rss>

