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: 
Read only

Table Sorting

Former Member
0 Likes
1,084

Hello,

I have came with an issue of report in which i have to sort the entries in the table according to some particular entries in a column which as fixed just 5 values in string.Using a simple Sort command I m not able to perform the sort as I required, Please Kindly Help me for the same.

1 ACCEPTED SOLUTION
Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,057

No ABAP statement will sort those data as you liked it as

Character-like components are sorted by default by their binary representation (code page). Textual sorting of character-like components can be performed using the addition AS TEXT.

(AFAIK no code page has this order BRYGW, those good old characters from birth of ASCII were always sorted in Latin alphabet order)

So either create another column in the internal table and fill it with 1 for B, 2 for R, etc, and sort with this field (not-displayed) or replace temporary the color codes by values in your required order, sort the table, and replace back with the original color codes.

Now you should understand the use of special values in lights_fieldname in ALV grid

'1' = red traffic light

'2' = yellow traffic light

'3' = green traffic light

You have to code yourself your own values as you have 2 other values to manage.

Regards,

Raymond

9 REPLIES 9
Read only

Former Member
0 Likes
1,057

Are you giving the correct order in sorting.

SORT itab BY field1 field2 .. ( use AS TEXT) addition if you want character sorting.

more details.

http://help.sap.com/saphelp_erp60_sp/helpdata/en/fc/eb3800358411d1829f0000e829fbfe/content.htm

Read only

0 Likes
1,057

Hi,

Thanks for replying,But in my internal table I have a column which contains list of particular colors just five of them used to define zones as Yellow,White,Green,Red &Black. when I use the ascending order it gives me BRGWY and in descending YWGRB colors format. But I want it as in format Black Red Yellow Green White.So the above syntax does not works.Please suggest me any other solution.  

Read only

0 Likes
1,057

Hi

then try

SORT itab BY Black Red Yellow Green White.

Black Red Yellow Green White. will have the columns to be sorted

Thanks.

Read only

0 Likes
1,057

Hi sorry i couldnt get your question are those colors contents of a particular column?

Read only

0 Likes
1,057
Hi Ajinkya,
Add an extra column in your internal table with serial number against the colors and sort with the serial number.Check the test program below.

TYPES:BEGIN  OF s_test,
             col       TYPE char2,
             sorder TYPE n,
             END    OF s_test.


DATA:it_test TYPE STANDARD TABLE OF s_test,
           wa_test TYPE                                      s_test.


wa_test-col     = 'W'.
wa_test-sorder  = 5.
APPEND wa_test TO it_test.

wa_test-col = 'Y'.
wa_test-sorder  = 3.
APPEND wa_test TO it_test.

wa_test-col = 'G'.
wa_test-sorder  = 4.
APPEND wa_test TO it_test.

wa_test-col = 'B'.
wa_test-sorder  = 1.
APPEND wa_test TO it_test.

wa_test-col = 'R'.
wa_test-sorder  = 2.
APPEND wa_test TO it_test.

SORT it_test BY col.

LOOP AT it_test INTO wa_test.
  WRITE:/5 wa_test-col.
ENDLOOP.

ULINE.

SORT it_test BY sorder.

LOOP AT it_test INTO wa_test.
  WRITE:/5 wa_test-col.
ENDLOOP.

ULINE.

With regards,

Read only

0 Likes
1,057

Hi

Ajinkya

Some text disn posted on my last reply....

add one more column with integer then modify ur internal table as

Black with 1

Red with 2

Yellow with 3

Green with 4

White.with 5  on the new interger column

now sort with the new interger colum

SORT itab BY interger.

Thanks

Ben

Read only

kumud
Active Contributor
0 Likes
1,057

Hello Ajinkya,

What would you do with the internal table once it is sorted in the order you want? Thanks!

Regards,

Kumud

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,058

No ABAP statement will sort those data as you liked it as

Character-like components are sorted by default by their binary representation (code page). Textual sorting of character-like components can be performed using the addition AS TEXT.

(AFAIK no code page has this order BRYGW, those good old characters from birth of ASCII were always sorted in Latin alphabet order)

So either create another column in the internal table and fill it with 1 for B, 2 for R, etc, and sort with this field (not-displayed) or replace temporary the color codes by values in your required order, sort the table, and replace back with the original color codes.

Now you should understand the use of special values in lights_fieldname in ALV grid

'1' = red traffic light

'2' = yellow traffic light

'3' = green traffic light

You have to code yourself your own values as you have 2 other values to manage.

Regards,

Raymond

Read only

0 Likes
1,057

Thank U all for the reply finally i have got the solution.