Skip to Content

Sort Itab

Hello friends,

Can anyone tell me how will this statement behave

Sort itab by field1

where my itab has 2 fields 1 is customer no and 2 is description.

Now , here the sort is sorting the itab by fields 1 as well by description.

can anyone please tell me is this behavior normal for sap i:e after field one system will sort itab by field 2 also without mentioning in the SORT statement.

Please help.

Thanks n Regards,

AShwini

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 08, 2007 at 12:02 PM

    Hi Ashwini,

    System will not automaticaly sort itab by field2 unless mentioned. It does happen if the entries in database tables are already in sorted form.

    Else you need to mention both fields in sorting.

    sort itab by field1 field2.

    Regards,

    Arati.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 08, 2007 at 12:19 PM

    Hi Ashwini,

    Sort itab by field1

    will sort the itab by field 1 i.e.,customer number.The other fields related to field1(i.e.description) also gets sorted automatically according to the key field (customer number).

    thanks,

    Shanmugapriya

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 08, 2007 at 12:19 PM

    Hi

    you can do that by

    sort itab by field1 feild2

    or

    simply sort itab

    Reward if usefull

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 08, 2007 at 07:50 PM

    Hi,

    when you use sort itab field1 field2 ... fieldn (you can use optional ascending or descending)

    you specific the priority sort to your table.

    Example:

    Field1 Field2 Field3

    A........104........2

    A........105........2

    A........106........6

    B........107........7

    B........108........7

    when you just use sort itab by field, the table is sorted by this field.

    Try to see the abap help about sort, there are useful samples here.

    Regards.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 09, 2007 at 05:37 AM

    Hi,

    supposse the IT has the following records,

    cust-id cust-desc

    104 doctor

    101 doctor

    105 student

    102 merchant.

    SORT tbl_cust by cust-id.

    then tbl_cust contain records like

    cust-id cust-desc

    101 doctor

    102 merchant.

    104 doctor

    105 student

    In above sort statement sorth the records a/c to cust-id in acending order.

    SORT tbl_cust by cust-desc.

    then tbl_cust contain records like

    cust-id cust-desc

    104 doctor

    101 doctor

    102 merchant.

    105 student

    In above sort statement the records a/c to cust-description in alphabetical order.

    SORT tbl_cust by cust-desc cust-id.

    then tbl_cust contain records like

    cust-id cust-desc

    101 doctor

    104 doctor

    102 merchant.

    105 student

    In above sort statement, the records first sort a/c to cust-desc

    104 doctor

    101 doctor

    then it sort agian cust-id in acending order.then finally it sort into

    101 doctor

    104 doctor

    hope you can understand with above examples.

    <b>Reward with points if helpful.</b>

    Regards,

    Vijay

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 10, 2007 at 11:32 AM

    It may be the case that your DB table (for where you are working on the internal table) is a sorted table and the sorted field is by default Field 2.

    Hence, in your case, its better to write the sort statement as :

    <b>sort itab by field 1 field 2.</b>

    Reward if helpful,

    Thanks and Regards,

    Anindita

    Add comment
    10|10000 characters needed characters exceeded