cancel
Showing results for 
Search instead for 
Did you mean: 

Sorting

Former Member
0 Kudos

Hi,

I wanted ti sort Itable by XBLNR and KUNNR, but it is sorting the MATNR also. Please help me why it is happening like this.

SORT gt_zprice BY xblnr DESCENDING kunnr.

For Ex:

kunnr matnr xblnr

1002 11111 SA-07-009

1002 22222 SA-07-009

1002 33333 SA-07-009

1001 11111 SA-07-008

1001 22222 SA-07-008

1001 33333 SA-07-008

I want the result to be like this

kunnr matnr xblnr

1001 11111 SA-07-008

1001 22222 SA-07-008

1001 33333 SA-07-008

1002 11111 SA-07-009

1002 22222 SA-07-009

1002 33333 SA-07-009

But it is comming like the following, material is also sorting.

kunnr matnr xblnr

1001 22222 SA-07-008

1001 11111 SA-07-008

1001 33333 SA-07-008

1002 22222 SA-07-009

1002 11111 SA-07-009

1002 33333 SA-07-009

Thanks,

Veni.

Accepted Solutions (1)

Accepted Solutions (1)

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Does this not work? Adding the word STABLE?



SORT  gt_zprice STABLE BY xblnr DESCENDING kunnr ASCENDING

Regards,

Rich Heilman

Former Member
0 Kudos

Hi Rich,

Thank you.

STABLE did the magic, it worked. But can you please give little info on STABLE.

Thanks,

Veni.

Answers (3)

Answers (3)

ferry_lianto
Active Contributor
0 Kudos

Hi Veni,

From online help.

You can use STABLE to ensure a stable sort order. This means that the order of lines, which is the same in the sort key, remains unchanged after sorting. Without the STABLE addition, the order is not preserved and repeated sorting of a table by the same sort key changes the order each time the table is sorted.

Regards,

Ferry Lianto

Former Member
0 Kudos

Thank you Ferry.

Regards,

Veni.

Former Member
0 Kudos

Use

SORT gt_zprice BY xblnr kunnr matnr.

Hope it works.

Lokesh

<b><REMOVED BY MODERATOR></b>

Message was edited by:

Alvaro Tejada Galindo

Former Member
0 Kudos

Hi Naimesh and Lokesh,

Sorting by 2 fields are sorting matnr also.

Sorting by 3 fields are working, but why it is sorting the matnr when I was only sorting on xblnr and kunnr.

Thanks,

Veni.

Former Member
0 Kudos

If your see the result, it is basically not sorting on Matnr at all. Matnr field values are neither in ascending nor in descending order.

But yes it did change the sequence of your input. Dont know the reason why?

Does sorting on the 3rd field affect your output, if yes then you might have to try some crude way of sorting rather than using the SORT statement.

Hope it helps.

Lokesh

Message was edited by:

Lokesh Aggarwal

Former Member
0 Kudos

Check the below program :

data : begin of gt_zprice occurs 0,

kunnr like kna1-kunnr,

matnr like mara-matnr,

xblnr like vbrk-xblnr,

end of gt_zprice.

start-of-selection.

gt_zprice-kunnr = '1002'.

gt_zprice-matnr = '11111'.

gt_zprice-xblnr = 'SA-07-009'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1002'.

gt_zprice-matnr = '22222'.

gt_zprice-xblnr = 'SA-07-009'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1002'.

gt_zprice-matnr = '33333'.

gt_zprice-xblnr = 'SA-07-009'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1002'.

gt_zprice-matnr = '11111'.

gt_zprice-xblnr = 'SA-07-009'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1001'.

gt_zprice-matnr = '11111'.

gt_zprice-xblnr = 'SA-07-008'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1001'.

gt_zprice-matnr = '22222'.

gt_zprice-xblnr = 'SA-07-008'.

append gt_zprice.

clear gt_zprice.

gt_zprice-kunnr = '1001'.

gt_zprice-matnr = '33333'.

gt_zprice-xblnr = 'SA-07-008'.

append gt_zprice.

clear gt_zprice.

<b>SORT gt_zprice BY xblnr ASCENDING kunnr DESCENDING matnr ascending.</b>

loop at gt_zprice.

endloop.

Now you get output as you expected.

Thanks

Seshu

naimesh_patel
Active Contributor
0 Kudos

Hello Veni,

Add ASCENDING after the kunnr.

Like:

SORT gt_zprice BY xblnr DESCENDING kunnr ASCENDING

.

Regards,

Naimesh Patel