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

Sort table by field position?

Former Member
0 Likes
668

Hello,

Is it possible to sort a table by field position? For example, I have table ITAB with a 4 character field F1. I want to sort the table first by the 3rd position ( itab-f12(1) ) ascending then the 4th position ( itab-f13(1) ) descending.

data: begin of itab occurs 0,

f1(4),

end of itab.

itab-f1 = '01AC'.

append itab.

itab-f1 = '02AB'.

append itab.

itab-f1 = '01CD'.

append itab.

itab-f1 = '02CA'.

append itab.

sort itab...

I thought about using TRANSLATE with a rule and converting each position to a numeric value (then transfer the value to a new 'sort' field), but I wasn't sure if there was an easier way.

The sorted table should read: '01AC' ; '02AB' ; '01CD' ; '02CA'

Any help is appreciated.

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
632

Hi,

Please try this.


DATA: BEGIN OF ITAB OCCURS 0,
        F1(4),
      END OF ITAB.
                                                                        
DATA: BEGIN OF ITAB2 OCCURS 0,
        F1(1),
        F2(1),
        F3(4),
      END OF ITAB2.
                                                                        
ITAB-F1 = '01AC'.
APPEND ITAB.
                                                                        
ITAB-F1 = '02AB'.
APPEND ITAB.
                                                                        
ITAB-F1 = '01CD'.
APPEND ITAB.
                                                                        
ITAB-F1 = '02CA'.
APPEND ITAB.

LOOP AT ITAB.
  ITAB2-F1 = ITAB-F1+2(1).
  ITAB2-F2 = ITAB-F1+3(1).
  ITAB2-F3 = ITAB-F1.
  APPEND ITAB2.
ENDLOOP.
                                                                        
SORT ITAB2 BY F1 ASCENDING
              F2 DESCENDING.
                                                                        
LOOP AT ITAB2.
  WRITE: / ITAB2-F3.
ENDLOOP.

Regards,

Ferry Lianto

4 REPLIES 4
Read only

ferry_lianto
Active Contributor
0 Likes
633

Hi,

Please try this.


DATA: BEGIN OF ITAB OCCURS 0,
        F1(4),
      END OF ITAB.
                                                                        
DATA: BEGIN OF ITAB2 OCCURS 0,
        F1(1),
        F2(1),
        F3(4),
      END OF ITAB2.
                                                                        
ITAB-F1 = '01AC'.
APPEND ITAB.
                                                                        
ITAB-F1 = '02AB'.
APPEND ITAB.
                                                                        
ITAB-F1 = '01CD'.
APPEND ITAB.
                                                                        
ITAB-F1 = '02CA'.
APPEND ITAB.

LOOP AT ITAB.
  ITAB2-F1 = ITAB-F1+2(1).
  ITAB2-F2 = ITAB-F1+3(1).
  ITAB2-F3 = ITAB-F1.
  APPEND ITAB2.
ENDLOOP.
                                                                        
SORT ITAB2 BY F1 ASCENDING
              F2 DESCENDING.
                                                                        
LOOP AT ITAB2.
  WRITE: / ITAB2-F3.
ENDLOOP.

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
632

I thought of that solution, but I was trying for something that doesn't require a 'work table' or translating based on a rule. It may not be possible.

Read only

ferry_lianto
Active Contributor
0 Likes
632

Hi Cal,

I don't think possible without creating another internal table.

Regards,

Ferry Lianto

Read only

0 Likes
632

Hi,

You cannot sort using some characters of the field. You will need internal table to sort it.