User-specified dynamic sorting
This code allows the user to numerically specify the sort order of a report from the selection screen.
This piece of code allows the user to numerically specify the sort order of a report from the selection screen. It was written in v.4.6, but I have written and used similiar versions in 3.1H. All the code around the dynamic search will work in either version. It should work in any version though.
REPORT YSORT NO STANDARD PAGE HEADING LINE-SIZE 132 LINE-COUNT 65. *----------------------------------------------------------------------- * Name: YSORT * Created by: Eddie Bottger * Created on: 07/30/2002 * * Description: This program will four fields from the customer * master table. It provides four input fields for * the user to input a number. The report then sorts * the output list by the fields specified in the order * specified. For example if the user inputs a 1 under * Name and a 2 under Country, the report would be sorted * by name then by country. *----------------------------------------------------------------------* * T A B L E S * *----------------------------------------------------------------------* TABLES: KNA1. "General Data in Customer Master *----------------------------------------------------------------------* * C O N S T A N T S * *----------------------------------------------------------------------* CONSTANTS: C_SORT(4) TYPE C VALUE 'SORT', C_SORTNUMS(11) TYPE C VALUE ' 0123456789'. *----------------------------------------------------------------------* * D A T A D E F I N I T I O N S * *----------------------------------------------------------------------* TYPES: BEGIN OF TTAB, KUNNR LIKE KNA1-KUNNR, LAND1 LIKE KNA1-LAND1, NAME1 LIKE KNA1-NAME1, REGIO LIKE KNA1-REGIO, END OF TTAB, BEGIN OF TSORT, NUMB(1) TYPE C, END OF TSORT. DATA: ITAB TYPE TTAB OCCURS 0 WITH HEADER LINE, ISORT TYPE TSORT OCCURS 0 WITH HEADER LINE. * Containers for field names. Since we allow for four diiferent fields * to be sorted on we have to have four containers. They need to be * defined long enough to hold the longest field name. All our fields * names i.e. "regio" are 5 characters long DATA: SORT1(5) TYPE C, SORT2(5) TYPE C, SORT3(5) TYPE C, SORT4(5) TYPE C. *----------------------------------------------------------------------* * S E L E C T I O N S C R E E N * *----------------------------------------------------------------------* SELECT-OPTIONS: S_KUNNR FOR KNA1-KUNNR. SELECTION-SCREEN SKIP. SELECTION-SCREEN BEGIN OF BLOCK SORT WITH FRAME TITLE TEXT-T01. "Sort Order SELECTION-SCREEN COMMENT /3(08) TEXT-S01. "Customer SELECTION-SCREEN COMMENT 11(04) TEXT-S02. "Name SELECTION-SCREEN COMMENT 20(07) TEXT-S03. "Country SELECTION-SCREEN COMMENT 29(04) TEXT-S04. "City SELECTION-SCREEN BEGIN OF LINE. SELECTION-SCREEN POSITION 5. PARAMETERS P_KUNNR(1) TYPE C DEFAULT '1'. SELECTION-SCREEN POSITION 12. PARAMETERS P_NAME1(1) TYPE C DEFAULT '2'. SELECTION-SCREEN POSITION 22. PARAMETERS P_LAND1(1) TYPE C. SELECTION-SCREEN POSITION 30. PARAMETERS P_REGIO(1) TYPE C. SELECTION-SCREEN END OF LINE. SELECTION-SCREEN SKIP. * ascending & descending sort options SELECTION-SCREEN: BEGIN OF LINE. SELECTION-SCREEN COMMENT 3(12) TEXT-CM9. "Sort Report SELECTION-SCREEN POSITION 17. PARAMETERS ASCEND RADIOBUTTON GROUP SORT DEFAULT 'X'. SELECTION-SCREEN COMMENT 19(18) TEXT-CA1. "Ascending SELECTION-SCREEN POSITION 41. PARAMETERS DESCEND RADIOBUTTON GROUP SORT. SELECTION-SCREEN COMMENT 43(18) TEXT-CD1. "'Descending SELECTION-SCREEN: END OF LINE. SELECTION-SCREEN END OF BLOCK SORT. * AT SELECTION-SCREEN ON BLOCK SORT. PERFORM SORT_CHECK. START-OF-SELECTION. SELECT KUNNR LAND1 NAME1 REGIO INTO TABLE ITAB FROM KNA1 WHERE KUNNR IN S_KUNNR. IF SY-SUBRC NE 0. WRITE:/ 'No customers exist within that range'. EXIT. ENDIF. END-OF-SELECTION. IF DESCEND = 'X'. SORT ITAB DESCENDING BY (SORT1) (SORT2) (SORT3) (SORT4). ELSE. SORT ITAB ASCENDING BY (SORT1) (SORT2) (SORT3) (SORT4). ENDIF. LOOP AT ITAB. WRITE:/ SY-VLINE, 2(10) ITAB-KUNNR, 14(01) SY-VLINE, 16(35) ITAB-NAME1, 52 SY-VLINE, 54(07) ITAB-LAND1, 62 SY-VLINE, 64(03) ITAB-REGIO, 69 SY-VLINE. ENDLOOP. ULINE AT (69). TOP-OF-PAGE. FORMAT INTENSIFIED ON COLOR COL_HEADING. ULINE AT (69). WRITE:/ SY-VLINE, (10) 'Customer', SY-VLINE, (35) 'Name', SY-VLINE, (07) 'Country', SY-VLINE, (04) 'City', SY-VLINE. ULINE AT (69). FORMAT INTENSIFIED OFF. *&---------------------------------------------------------------------* *& Form SORT_CHECK *&---------------------------------------------------------------------* FORM SORT_CHECK. DATA: FIELD_NAME(10) TYPE C. CLEAR ISORT. REFRESH ISORT. PERFORM SET_SORT USING P_KUNNR FIELD_NAME 'KUNNR'. PERFORM SET_SORT USING P_NAME1 FIELD_NAME 'NAME1'. PERFORM SET_SORT USING P_LAND1 FIELD_NAME 'LAND1'. PERFORM SET_SORT USING P_REGIO FIELD_NAME 'REGIO'. ENDFORM. " SORT_CHECK *&---------------------------------------------------------------------* *& Form SET_SORT *&---------------------------------------------------------------------* FORM SET_SORT USING P_NUMB P_FIELD VALUE(P_VALUE). FIELD-SYMBOLS: <A>. * This makes sure the user has input nothing but numerics or space. * class from your system and a valid number. IF P_NUMB CN C_SORTNUMS. * You will need to replace the message ID and number with a message MESSAGE ID 'ZR' TYPE 'E' NUMBER '223' WITH 'Sort options must be 0-9'. ELSEIF P_NUMB NE SPACE. READ TABLE ISORT WITH KEY NUMB = P_NUMB BINARY SEARCH. IF SY-SUBRC EQ 0. MESSAGE ID 'ZR' TYPE 'E' NUMBER '223' WITH 'Can not Sort different fields in same order.'. ELSE. MOVE P_NUMB TO ISORT-NUMB. APPEND ISORT. SORT ISORT ASCENDING BY NUMB. ENDIF. * This moves the field name into the sort container. For example the * user puts '1' in the p_name1 field, meaning they want to sort by * customer name first. The field SORT1 would hold the value NAME1. CONCATENATE C_SORT P_NUMB INTO P_FIELD. ASSIGN (P_FIELD) TO <A>. MOVE P_VALUE TO <A>. ENDIF. ENDFORM. " SET_SORT