Home > SAP Tips > ABAP/Java developer tips > User-specified dynamic sorting
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ABAP/JAVA DEVELOPER TIPS

User-specified dynamic sorting


Eddie Bottger
08.08.2002
Rating: -3.93- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.



Code

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


Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ABAP/Java developer tips
Select Text fields: Case-insensitive
Finding BADIs you can use
Is this the quickest way to find a BADI?
ABAP Objects in SAP Workflow to provide improved performance
Easily debug error messages in SAP processes
Accessing private attributes in ABAP Objects
Find a BADI in a minute
Top 10 SAP tips of 2007
How to transport an SAP query in R/3 4.6x
How to switch off message determination in BAPI_PO_CREATE1

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsBlogsTipsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts