Application Development and Automation 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: 

Problem is Sorting Numeric with Decimals

Former Member
0 Kudos
601

Hi leads,

I am getting problem in sorting when I sort on numeric values with decimals.

Here I am giving my code.

TYPES : BEGIN OF ty_sort,
         val1 TYPE char10,
         val2 TYPE char10,
        END OF ty_sort.

DATA : it  TYPE STANDARD TABLE OF ty_sort,
       it2 TYPE STANDARD TABLE OF ty_sort,
       wa TYPE ty_sort.

wa-val1 = '0.05'.
wa-val2 =  '0.00'.
APPEND wa TO it.
CLEAR wa.

wa-val1 = '0.03'.
wa-val2 =  '0.02'.
APPEND wa TO it.
CLEAR wa.

wa-val1 = '0.00'.
wa-val2 =  '0.06'.
APPEND wa TO it.
CLEAR wa.

SORT it BY val1 DESCENDING val2 DESCENDING.

LOOP AT it2 INTO wa.
  WRITE : / wa-val1,  20 wa-val2.
  CLEAR wa.
ENDLOOP.

O/P : ( I am getting this output, which is not expected )

0.05 0.00

0.03 0.02

0.00 0.06

Expected O/P :

0.05 0.06

0.03 0.02

0.00 0.00

Thanks,

Sandeep

Moderator message - Please use code tags around your code

Edited by: Rob Burbank on Nov 25, 2009 3:17 PM

3 REPLIES 3

Former Member
0 Kudos
312

The problem is with data declaration :

TYPES : BEGIN OF ty_sort,

val1 TYPE char10,

val2 TYPE char10,

END OF ty_sort.

Declare val1 and val2 as type p decimals 2 instead of char10 ...

Check your requirement as well ... I don't think this is correct ...

Shafiq_Rehman
Active Contributor
0 Kudos
312

This is exactly how sort works. Your requirement is wrong, if you still want to achieve that, then you cannot use one internal table for this purpose.

First append 3 values in ITAB1, then rest three values in ITAB2. ITAB1 and ITAB2 will have only one column. Then sort both tables, and append them in 3rd intenal table which will have 2 columns, you know what I mean?

You know I am bored and have some time right now, so I'll write the code for you ....


REPORT  zsr_test13.
TYPES: BEGIN OF ty_type1,
         value TYPE char10,
       END OF ty_type1.
TYPES: BEGIN OF ty_sort,
        val1 TYPE char10,
        val2 TYPE char10,
       END OF ty_sort.

DATA : it1  TYPE STANDARD TABLE OF ty_type1,
       it2 TYPE STANDARD TABLE OF ty_type1,
       it3 TYPE STANDARD TABLE OF ty_sort,
       wa_it3 TYPE ty_sort.
DATA: temp_value TYPE char10.

APPEND '0.05' TO it1.
APPEND '0.03' TO it1.
APPEND '0.00' TO it1.

APPEND '0.00' TO it2.
APPEND '0.02' TO it2.
APPEND '0.06' TO it2.


SORT it1 DESCENDING BY value.
SORT it2 DESCENDING BY value.

DO 3 TIMES.
  READ TABLE it1 INDEX sy-index INTO temp_value.
  wa_it3-val1 = temp_value.
  READ TABLE it2 INDEX sy-index INTO temp_value.
  wa_it3-val2 = temp_value.
  APPEND wa_it3 TO it3.
ENDDO.

LOOP AT it3 INTO wa_it3.
  WRITE:/ wa_it3-val1,  20 wa_it3-val2.
ENDLOOP.

Former Member
0 Kudos
312

Hi Sandeep,

Change your data declaration.Take help on data types.