Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting amounts and quantities in ABAP

0 Kudos
710

Hi,

Is there a Function module to sort amounts and quantities respecting different currencies (or Units) in an Internal table. I have an internal table where one field is the amount numeral value and another field is the currency. I need to sort the entire table based on the currency.

Regards,

Venu

7 REPLIES 7

Former Member
0 Kudos
343

Hi

You can sort the table using both fields:

SORT ITAB BY <CURRENCY> <AMOUNT>

Max

0 Kudos
343

Yes but that wont solve my problem. imagine i have the below entries

10    INR

2      EUR

15    INR

150  INR

1     EUR

by sorting with two fields i get

1     EUR

2     EUR

10   INR

15   INR

150 INR

But if i see the value of each amount its not sorted in the right order.

0 Kudos
343

and which is the order you want?

Max

0 Kudos
343

based on the conversion rate, assuming 1 EUR = 80 INR

10     INR

15     INR

1       EUR

150    INR

2       EUR

0 Kudos
343

Hi Venu,

You have to convert the quantity values into a common currency and sort accordingly.

For eg: Add 1 more field in your internal table with name 'Equal' and populate the quantity values into Equal field by converting into INR as  below.

Qty   Curr     Equal

10      INR          10

15      INR          15

1        EUR         80

150    INR       150

2        EUR       160

Sort your internal table by Equal.

0 Kudos
343

Hi

That means you need an additional field which amount converted to the same currency

DATA: BEGIN OF ITAB OCCURS 0,

             AMOUNT,

             CURRENCY,

            AMOUNT_CONVERTED,

            .................

SORT ITAB BY AMOUNT_CONVERTED

You can use fm CONVERT_TO_FOREIGN_CURRENCY or CONVERT_TO_LOCAL_CURRENCY to convert the amount

But this solution can be used for amount, because the dimension of the value is the same, you can have some problem with the quantity:

You can't sort the Hour and weight together...for example

Max

Former Member
0 Kudos
343

Hi Venu,

Try using

CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'

    EXPORTING

      date             =                            ====> date to be mentioned

      foreign_amount   =                   ====> Give the Amount to which to currency

      foreign_currency = 'GBP'          ====>To Currency

      local_currency   = 'USD'           ====>From Currency

      type_of_rate     = 'M'                ====> Type of rate (Month begin type)

    IMPORTING

      exchange_rate    = cv_lv_ukurs =====> Exchnage rate on the day

      local_amount     = cv_vallc1      =====> Amount after calculation amt

    EXCEPTIONS

      no_rate_found    = 1

      overflow         = 2

      no_factors_found = 3

      no_spread_found  = 4

      derived_2_times  = 5

      OTHERS           = 6.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.

Do a sort after this.If you want exactly the EUR also after the conversion keep a flag in the loop and capture the index.Once after the conversion you can replace the INR with Euro.

Regards,

Kannan