‎2007 Mar 16 1:48 PM
Hi all,
I have an internal table say itab.
In itab i have fld1 , fld2 , fld3 .
itab fld2 can have a, b, c, d
say for this the data
fld1 fld2 fld3
2wqq b 2
2wqq a 2
w33 d 3
after sorting i need to hav in this order
w33 d 3
2wqq b 2
2wqq a 2
i always have to sort based on fld 2 and in this order d,b,a,c
say if i don't have record with b then the order should be d ,a , c
let me know how to do this
Thanks
‎2007 Mar 16 2:38 PM
Hi,
Please try this ...
REPORT ZTEST.
DATA: BEGIN OF ITAB OCCURS 0,
SORT(1),
FIELD1(4),
FIELD2(1),
FIELD3(1).
DATA: END OF ITAB.
ITAB-FIELD1 = '2WQQ'.
ITAB-FIELD2 = 'B'.
ITAB-FIELD3 = '2'.
APPEND ITAB.
ITAB-FIELD1 = '2WQQ'.
ITAB-FIELD2 = 'A'.
ITAB-FIELD3 = '2'.
APPEND ITAB.
ITAB-FIELD1 = 'W33'.
ITAB-FIELD2 = 'D'.
ITAB-FIELD3 = '3'.
APPEND ITAB.
LOOP AT ITAB.
CASE ITAB-FIELD2.
WHEN 'B'.
ITAB-SORT = 1.
WHEN 'A'.
ITAB-SORT = 2.
WHEN 'D'.
ITAB-SORT = 3.
WHEN 'C'.
ITAB-SORT = 4.
WHEN OTHERS.
ENDCASE.
MODIFY ITAB.
ENDLOOP.
SORT ITAB BY SORT ASCENDING.
LOOP AT ITAB.
WRITE: / ITAB-FIELD1, ITAB-FIELD2, ITAB-FIELD3.
ENDLOOP.
Regards,
Ferry Lianto
‎2007 Mar 16 1:52 PM
‎2007 Mar 16 1:52 PM
sort itab descending by otab.
Each line of the table otab defined a component of the sort key.
regards,
bharat.
‎2007 Mar 16 1:54 PM
Hi,
You can sort by a field either ASCENDING or DESCENDING
A,b,c,d can be sorted either d,b,c,a or a,b,c,d .
but not like what you said.
Regards,
Anji
‎2007 Mar 16 2:03 PM
Hi Preeti,
Interestiing sort order..Following is what I would suggest...
1. create a string with your sort order '......dbac'...to include all the characters you might have in text...lets call it lv_sort_sequence... create another string with normal sort sequence '0123..abcd...z.'..lets call it lv_actual_sequence.
2. Replace every character in your table..eg: replace lv_sort_sequence(1 to 36) in your table with lv_actual_sequence(1 to 36). if you consider only dbac...replace d with a, b with b, a with c etc... I hope I am clear here.
3. Now sort your data in ascending order.
4. Do the reverse of step 2. Replace lv_actual_sequence(1-36) with lv_sort_sequence(1-36)
Thanks.
Srinivas.
‎2007 Mar 16 2:12 PM
Hi Srinivas,
in fact my sorting field doesn't contain only one char .
its like this,
affff
cgfff
bhggg
dhhhh
then i want to print the records in this order
bhggg
affff
dhhhh
cgfff
i know that allt he the sort field contains only the above 4 values
will that help
let em know how to achieve this
‎2007 Mar 16 2:27 PM
I am not sure if I understood you correctly...
Are you saying you need to consider only the first column for sorting...
then replace only first columns.
I was asking you to replace your entire table with corresponding mapped characters, then sort and replace back.
‎2007 Mar 16 2:42 PM
Hi Srinivas,
My actual data will be like this
fld2 can have only anyone these four values
bgfff
ahgyyy
ddsds
cfghy
ex 1)
fld1 fld2 fld3
2wqq bgfff 2
2wqq ahgyyy 2
w33 ddsds 3
after sorting i need to have in this order
w33 bgfff 3
2wqq ddsds 2
2wqq ahgyyy 2
ex2)
909 bgfff 2
456 ahgyyy 2
908 cfghy 1
544 ddsds 3
after sorting i need to have in this order
909 bgfff 2
544 ddsds 3
908 cfghy 1
456 ahgyyy 2
sometimes this may happen
ex3)
909 bgfff 2
456 ahgyyy 2
556 ddsds 9
908 cfghy 1
544 ddsds 3
after sorting i need to have in this order
909 bgfff 2
544 ddsds 3
556 ddsds 9
908 cfghy 1
456 ahgyyy 2
or
909 bgfff 2
556 ddsds 9 --
544 ddsds 3 -- this two can switch that's not important
908 cfghy 1
456 ahgyyy 2
Hope you have clear idea now.
ANy help in achieving would be appreciated
Thanks
‎2007 Mar 16 2:56 PM
If its only any of those 4 values...first replace them with 1,2,3,4..sort data and then replace them back.
Will this satisfy your purpose.
‎2007 Mar 16 2:38 PM
Hi,
Please try this ...
REPORT ZTEST.
DATA: BEGIN OF ITAB OCCURS 0,
SORT(1),
FIELD1(4),
FIELD2(1),
FIELD3(1).
DATA: END OF ITAB.
ITAB-FIELD1 = '2WQQ'.
ITAB-FIELD2 = 'B'.
ITAB-FIELD3 = '2'.
APPEND ITAB.
ITAB-FIELD1 = '2WQQ'.
ITAB-FIELD2 = 'A'.
ITAB-FIELD3 = '2'.
APPEND ITAB.
ITAB-FIELD1 = 'W33'.
ITAB-FIELD2 = 'D'.
ITAB-FIELD3 = '3'.
APPEND ITAB.
LOOP AT ITAB.
CASE ITAB-FIELD2.
WHEN 'B'.
ITAB-SORT = 1.
WHEN 'A'.
ITAB-SORT = 2.
WHEN 'D'.
ITAB-SORT = 3.
WHEN 'C'.
ITAB-SORT = 4.
WHEN OTHERS.
ENDCASE.
MODIFY ITAB.
ENDLOOP.
SORT ITAB BY SORT ASCENDING.
LOOP AT ITAB.
WRITE: / ITAB-FIELD1, ITAB-FIELD2, ITAB-FIELD3.
ENDLOOP.
Regards,
Ferry Lianto
‎2007 Mar 16 4:09 PM
Hi,
I got that fixed based on Ferry example.
Thanks All
i awarded points to all
thanks again for your help
‎2007 Mar 16 2:43 PM
Try this
* In your table
TYPES: BEGIN of ztype,
field1(10),
field2(10),
field3(10),
* Add a sort field
my_sort type i,
END of ztype.
DATA: ztab type table of ztype with header line.
* Declare the sort ordre
DATA: my_order(4) value 'DCBA'.
* Fill the sort field
LOOP AT ztab.
SEARCH my_order for ztab-field2(1).
ztab-my_sort = sy-fdpos.
MODIFY ztab.
ENDLOOP.
* Then you can sort your table Regards
‎2007 Mar 16 3:05 PM
Hi,
Please check above my sample code using case statement.
I think it will work for any your cases.
Regards,
Ferry Lianto