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: 

Sort Char Columm

Former Member
0 Kudos
120

Hi all,

I have a columm with type char4 of a internal table. I need to sort it but i am unble to do the same.

Any 1 can help me on this as i have tried almost al the ways mention in SDN.

exapmle of values i have.

1

11

14

2

20

6

7

9

thanks in advance

Anuj

12 REPLIES 12

former_member555112
Active Contributor
0 Kudos
81

Hi,

Add one more column of type i to your internal table and move the numerals in the same and then sort on this column.

But only if you are sure that the data will contain numerals else it will lead to a dump.

Regards,

Ankur Parab

Former Member
0 Kudos
81

Hi Anuj

Sort itab by <columnname> ASCENDING/DESCENDING

MarcinPciak
Active Contributor
0 Kudos
81

Alternatively set this field as n field, or add a new one:


data: n_field(3) type n.

n_field = char_field.

sort itab by n_field.

example:

001

002

003

...

011

...

023

...

999

Regards

Marcin

Former Member
0 Kudos
81

hi !

Check it.

TYPES:BEGIN OF s,

no(4) TYPE c,

END OF s.

data:itab TYPE STANDARD TABLE OF s WITH HEADER LINE.

itab-no = 10.

APPEND itab.

itab-no = 5.

APPEND itab.

itab-no = 3.

APPEND itab.

itab-no = 1.

APPEND itab.

sort itab by no.

loop at itab.

write : / , itab-no.

ENDLOOP.

Output:

1

3

5

10

Thanks

Former Member
0 Kudos
81

Hi,

As suggested by Ankur you can add that numeric field when you are preparing your internal table and for each entry just increment the value of the field... Sort it accordingly.. and during display you may not display this field....

Former Member
0 Kudos
81

Hi Anuj,

No need to add num field.

Simply sort using STABLE option.

SORT lt_test STABLE BY val .

TYPES : BEGIN OF t_test,
            val TYPE char4,
          END OF t_test.

  DATA : lt_test TYPE STANDARD TABLE OF t_test,
         wa_test LIKE LINE OF lt_test.


    wa_test-val = 1.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 20.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 14.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 6.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 2.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 12.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

    wa_test-val = 3.
    APPEND wa_test TO lt_test.
    CLEAR wa_test.

      SORT lt_test STABLE BY val .
     LOOP AT lt_test INTO wa_test.
       WRITE wa_test-val.
     ENDLOOP.

Hope this will work.

Thanks,

Amol

0 Kudos
81

Hi all ,

thanks for your quick replies,

i have tried all these but it of no help, I cant introduce a new field in the internal table.

Amol i have also use stable but there is no sorting done.

guys sugest me more ways.

thanks.

Anuj

0 Kudos
81

Hi,

Then take into a new internal table and in that use the numeric/integer column

Sort the data and then pass back to the old internal table.

Regards,

Ankur Parab

0 Kudos
81

strange.

For me, it is displayed the output as below.

1    2    3    6   12   14   20

Can you try Copy the code into some test report and check the output?

Let me know the result.

Thanks,

Amol

Former Member
0 Kudos
81

Its simple Anuj.

Your length is char4 that means 4 characters , now if u have 1 then display it as 0001 and if its 11 display it as 0011 adn 0002 for 2 and 0012 for 12.

After adding 0's now if u will sort the fiels u will get the correct results......

Thanks Vijeta

0 Kudos
81

hi anuj,

why have u defined the field as char4 ? change it ur issue is solved.

MarcinPciak
Active Contributor
0 Kudos
81

>

> Hi all,

>

> I have a columm with type char4 of a internal table. I need to sort it but i am unble to do the same.

> Any 1 can help me on this as i have tried almost al the ways mention in SDN.

>

> exapmle of values i have.

>

> 1

> 11

> 14

> 2

> 20

> 6

> 7

> 9

>

> thanks in advance

> Anuj

What you really want to achieve? Do you want values be sorted as?:

1

2

6

7

9

11

14

20

or as you provided above?:

1

11

14

2

20

6

7

9

If the first applies simple sort completes this task. Copy and paste this code and ensure yourself:


DATA: BEGIN OF itab OCCURS 0,
        field TYPE char4,
      END OF itab.

itab-field = 20.
APPEND itab.

itab-field = 6.
APPEND itab.

itab-field = 7.
APPEND itab.

itab-field = 9.
APPEND itab.

itab-field = 2.
APPEND itab.

itab-field = 11.
APPEND itab.

itab-field = 14.
APPEND itab.

itab-field = 1.
APPEND itab.

SORT itab BY field.
LOOP AT itab.
  WRITE: / itab-field.
ENDLOOP.

Regards

Marcin