Application Development and Automation 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: 
Read only

Sort a column with a different format

preethi_santhanam
Participant
0 Likes
971

Hi All,

I need to sort entries of a particular format in ascending order. The entires are:

ABCD290710-0, ABCD290710-1, ABCD290710-10, ABCD290710-11, ABCD290710-2

When I use the Sort command, I get

ABCD290710-0

ABCD290710-1

ABCD290710-10

ABCD290710-11

ABCD290710-2

What I require is

ABCD290710-0

ABCD290710-1

ABCD290710-2

ABCD290710-10

ABCD290710-11

Could you pelase tell em a simple way to achieve this.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
902

It is how it behaves because it considers it as a string.

Thanks

Bala Duvvuri

7 REPLIES 7
Read only

Former Member
0 Likes
903

It is how it behaves because it considers it as a string.

Thanks

Bala Duvvuri

Read only

0 Likes
902

Hi Bala,

Thanks for your reply. I understand that the behaviour is correct as it considers it as a string, but is there any way, through which I can get my desired results?

Read only

0 Likes
902

it is a complicated process i think.

you need to split string at - into two variables .

convert the second variable into number and sort them and again append them to first variable

Thanks

Bal Duvvuri

Read only

0 Likes
902

Hi Bala,

Thanks for this approach. However, these numbers are a part of a column in alv. So, the process would get really too long.

Read only

0 Likes
902

Hi Spree,

I think we can not sort list you your requirement because SAP treats your records as string. has provided an alternative solutions for that, maybe you can create another ALV column and mark it as hidden. You can sort data by this column before show the result. I dont think the process would take too long performance.

If you find another solution, post here for us to reference.

Regards,

Read only

0 Likes
902

<div style="text-align:left!important">I understand that the behaviour is correct as it considers it as a string, but is there any way, through which I can get my desired results?</div>

In other programming languages we'd probably have some function/method that we can provide to the sort command to achieve our custom sort. E.g. in Java the interface [java.util.Comparator|http://java.sun.com/javase/6/docs/api/java/util/Comparator.html] could be used.

Now in ABAP we don't have that freedom. At best we could specify a locale to do a more textual type comparison (instead of the default byte code comparison done in strings, see addition [sort - as text|http://help.sap.com/abapdocu_70/en/ABAPSORT_ITAB.htm#!ABAP_ADDITION_3@3@]). Fiddling with offsets doesn't help, because we won't be able to get around the fact that you have still a string (but would like a numeric comparison).

If your requirement is integration with ALV, no additional column to use for sorting and output should remain as is, I'd consider using a custom [conversion exit|http://help.sap.com/abapdocu_70/en/ABENCONVERSION_EXITS.htm] for your field (i.e. define a corresponding data element/domain and utilize it). That's exactly what SAP does with conversion exit ALPHA, where any numbers are prefixed with leading zeros to allow a numeric comparison. However, since your case is special (and tries to combine string/numeric attributes in one field), you'd have to do that yourself.

Should you choose to go down that path, I'd make sure though, that the data format is fixed and should never change (obviously your conversion exit would need some restrictive assumptions which part is considered string and which part is considered number).

If you have no requirement to retain the existing data format, then it would probably best to store the data differently (or manipulate them while filling your internal table for the ALV), by adding the required 0-prefixes. E.g.

ABCD290710-00
ABCD290710-01
ABCD290710-10
...

Cheers, harald

p.s.: Introducing another (hidden) column is in my opinion problematic, because one would need to hook into the sort requests and automatically translate the specific field to the hidden field when doing the sort. Might be possible, but I don't think that's an easy solution (and would have to be done wherever that field is used, so possibly in multiple reports).

Read only

preethi_santhanam
Participant
0 Likes
902

Thank you all for your help. The issue is now solved.