‎2013 Jun 23 12:33 PM
Dear Gurus,
I would like to sort my internal table by Name (ascending), tried many things but no dice, any pointer on these?
my code as follows,
data : it_strave type standard table of stravelag,
it_sbook type standard table of sbook.
data : wa_strave type stravelag,
wa_sbook type sbook.
select * from sbook into CORRESPONDING FIELDS OF TABLE it_sbook where customID = '00000238'.
loop at it_sbook into wa_sbook.
select * from stravelag into corresponding fields of table it_strave where agencynum = wa_sbook-agencynum.
loop at it_strave into wa_strave.
SORT it_strave by Name ascending.
endloop.
write : / wa_strave-Name, wa_sbook-bookID, wa_sbook-agencyNum.
endloop.
question is how do I sort it by wa_strave-Name?
Thank you
‎2013 Jun 24 4:34 AM
Hi Norman,
Why do you loop it_strave?
If no value-addition is there, just perform sort it_strave
It is not right to write the sort statement inside the loop of the same internal table.
Regards.
‎2013 Jun 24 4:44 AM
Hi Norman,
Try like this...
DATA : it_strave TYPE STANDARD TABLE OF stravelag,
it_sbook TYPE STANDARD TABLE OF sbook.
DATA : wa_strave TYPE stravelag,
wa_sbook TYPE sbook.
SELECT * FROM sbook INTO CORRESPONDING FIELDS OF TABLE it_sbook WHERE customid = '00000238'.
SELECT * FROM stravelag INTO CORRESPONDING FIELDS OF TABLE it_strave for ALL ENTRIES IN it_sbook WHERE agencynum = it_sbook-agencynum.
SORT it_strave BY name ASCENDING.
LOOP AT it_strave INTO wa_strave.
WRITE : / wa_strave-name, wa_sbook-bookid, wa_sbook-agencynum.
ENDLOOP.
‎2013 Jun 24 4:50 AM
DATA : it_strave TYPE STANDARD TABLE OF stravelag,
it_sbook TYPE STANDARD TABLE OF sbook.
DATA : wa_strave TYPE stravelag,
wa_sbook TYPE sbook.
SELECT * FROM sbook INTO CORRESPONDING FIELDS OF TABLE it_sbook WHERE customid = '00000238'.
SELECT * FROM stravelag INTO CORRESPONDING FIELDS OF TABLE it_strave for ALL ENTRIES IN it_sbook WHERE agencynum = it_sbook-agencynum.
SORT it_strave BY name ASCENDING.
LOOP AT it_strave INTO wa_strave.
read table it_sbook into wa_sbook with key agencynum = wa_strave-agencynum BINARY SEARCH.
WRITE : / wa_strave-name, wa_sbook-bookid, wa_sbook-agencynum.
ENDLOOP.
‎2013 Jun 24 5:02 AM
Another Method..
TYPES : BEGIN OF ty_final,
name TYPE stravelag-name,
bookid TYPE sbook-bookid,
agencynum TYPE sbook-agencynum,
END OF ty_final.
DATA : it_strave TYPE STANDARD TABLE OF stravelag,
it_sbook TYPE STANDARD TABLE OF sbook,
it_final TYPE TABLE OF ty_final.
DATA : wa_strave TYPE stravelag,
wa_sbook TYPE sbook,
wa_final TYPE ty_final.
SORT : it_strave BY name,
it_sbook BY agencynum.
SELECT * FROM sbook INTO CORRESPONDING FIELDS OF TABLE it_sbook WHERE customid = '00000238'.
SELECT * FROM stravelag INTO CORRESPONDING FIELDS OF TABLE it_strave FOR ALL ENTRIES IN it_sbook WHERE agencynum = it_sbook-agencynum.
LOOP AT it_sbook INTO wa_sbook.
wa_final-bookid = wa_sbook-bookid.
wa_final-agencynum = wa_sbook-agencynum.
READ TABLE it_strave INTO wa_strave WITH KEY agencynum = wa_sbook-agencynum BINARY SEARCH.
IF sy-subrc = 0.
wa_final-name = wa_strave-name.
ENDIF.
APPEND wa_final TO it_final.
CLEAR wa_final.
ENDLOOP.
SORT it_final BY name.
LOOP AT it_final INTO wa_final.
WRITE : / wa_final-name, wa_final-bookid, wa_final-agencynum.
ENDLOOP.
‎2013 Jun 24 5:03 AM
Hi ,
More the one option:
- You can join the tables using sql by AGENCYNUM and sort by name.
- Some code:
*----------------------------------------------------------------------*
FORM test04 .
TYPES: BEGIN OF tp_join_1 .
INCLUDE TYPE stravelag AS tab_1 RENAMING WITH SUFFIX _t1 .
INCLUDE TYPE sbook AS tab_2 RENAMING WITH SUFFIX _t2 .
TYPES: END OF tp_join_1 .
DATA: it_join_1 TYPE TABLE OF tp_join_1 WITH HEADER LINE .
DATA: it_stravelag TYPE TABLE OF stravelag WITH HEADER LINE .
SELECT * FROM stravelag INTO TABLE it_stravelag.
SORT it_stravelag BY agencynum ."For BINARY SEARCH
DATA: it_sbook TYPE TABLE OF sbook WITH HEADER LINE .
SELECT * FROM sbook INTO TABLE it_sbook
WHERE
carrid EQ 'AA' .
LOOP AT it_sbook .
CLEAR it_join_1 .
READ TABLE it_stravelag
WITH KEY
agencynum = it_sbook-agencynum
BINARY SEARCH .
IF sy-subrc EQ 0 .
it_join_1-tab_1 = it_stravelag .
ENDIF .
it_join_1-tab_2 = it_sbook .
APPEND it_join_1 .
ENDLOOP .
SORT it_join_1 BY tab_1-name tab_2-fldate .
* Check the content of it_join_1
ENDFORM . "test04
*----------------------------------------------------------------------*
Regards.
CLEAR it_join_1 .
‎2013 Jun 24 7:39 AM
Hi Norman,
You can sort the text field of an Internal Table using the syntax:
SORT <Internal_Table_Name> By <Field_Name> AS TEXT.
where <Internal_Table_Name> is the Internal Table whose contents you want to sort,
<Field_Name> is the Field in the Internal Table on which you want to perform your sorting.
You need to first get all the values into an Internal Table say for eg: IT_FINAL.Then write the following statement :
SORT IT_FINAL By NAME AS TEXT.
Below is the Sample Report highlighlighting how the contents of the Internal Table are alphabetically sorted.
REPORT ZTEST_PRACTISE.
data : it_strave type standard table of stravelag,
it_sbook type standard table of sbook.
data : wa_strave type stravelag,
wa_sbook type sbook.
select * UP TO 10 rows from stravelag into table it_strave.
write: / 'Before Sorting' COLOR 3.
loop at it_strave into wa_strave.
write : / wa_strave-name.
endloop.
skip 3.
sort it_strave by name AS TEXT.
write: / 'After Sorting' COLOR 3.
loop at it_strave into wa_strave.
write : / wa_strave-name.
endloop.
Report Ouput:
‎2013 Jun 24 8:03 AM
Hi ,
To srt an internal table we just need to write a SORT statement.
It should not be in the loop like in your code above.
Just Write SORT <Internal table> ....field1,field2......
For more help please do F1 on SORT statement.
Regards,
Amit
‎2013 Jun 24 3:37 PM
Hi Guys,
thanks you for the input,
I've manage to resolve the sort.
Thank you!
‎2013 Jun 24 4:31 PM
Hi Norman,
Don't sort the internal table inside the loop.
Don't use the loop for sorting an itab.
Please check the code below.
select * from sbook into CORRESPONDING FIELDS OF TABLE it_sbook where customID ='00000238'.
loop at it_sbook into wa_sbook.
select * from stravelag into corresponding fields of table it_strave where agencynum = wa_sbook-agencynum.
endloop.
SORT it_starve ASCENDING BY NAME.
loop at it_strave into wa_strave.
READ TABLE IT_SBOOK into wa_sbook with key agencyNum = wa_starve-agencyNum.
write: / wa_strave-Name, wa_sbook-bookID, wa_sbook-agencyNum.
endloop.