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

Sorting Character string numerically

Former Member
0 Likes
6,616

Hey guys, I have an ALV Grid that needs to display a column of numbers with units at the end. the unit may change example Kg, mm, cm. Within the ALV, if they click on the sorting buttons, it does not sort correctly.It is because as a string it sorts differently from how numbers sort.. Example data: 1. 850,000 mm 2. 77,000 mm 3. 740,000 mm It sorts the data as : 1. 740,000mm 2.77,000 mm 3. 850,000 mm Instead it should sort data as: 1. 77,000mm 2.740,000 mm 3. 850,000 mm. Please help me. I have checked the following discussion, but was not helpful. https://scn.sap.com/thread/3429747 Regards Dilip S

1 ACCEPTED SOLUTION
Read only

rosenberg_eitan
Active Contributor
0 Likes
3,539

Hi,

For your knowledge the sorting is done using the internal format of the field .

For demonstration I formatted a date field using FUNCTION 'CONVERSION_EXIT_SDATE_OUTPUT' .

And I get:

Note that the months are sorted based on the actual value.


So it seems that you can format you internal data into a "sortable" format .

Using the exit you can present the internal data to the user as required .

Regards.

Eitan.

6 REPLIES 6
Read only

Ryan-Crosby
Active Contributor
0 Likes
3,539

Hi Dilip,

I would suggest adding an additional numeric column that represents each of the number values and then sort the ALV table by that field but make it a hidden field.  Treating the output as String instead of numeric will affect how the data is sorted.

Regards,

Ryan Crosby

Read only

0 Likes
3,539

Hi Ryan,

Thanks for your quick response.

Adding one more column to the ALV is not feasible in my situation. It would be great , if there ais any other way.

Thanks in advance.

Regards

Dilip S

Read only

rosenberg_eitan
Active Contributor
0 Likes
3,539

Hi,

I think that you can use Sorting Numeric Characters as numbers | SCN

as a base but in your case you need to format the data differently:

<unit measure> <SPACE> <numeric value> .

Regards.

Read only

Clemenss
Active Contributor
0 Likes
3,539

It sorts the data as :

  1. 740,000mm

  2.   77,000 mm

  3. 850,000 mm

Instead it should sort data as:

1. 77,000mm

2.740,000 mm

3. 850,000 mm

If the columns is of type QUAN for quantity field, it will sort as required.

Probably the ALV output field is type character.

Regards, Clemens

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,539

Dilip Kumar Subramaniam wrote:

I have checked the following discussion, but was not helpful. https://scn.sap.com/thread/3429747

(the thread is proposing a numeric field + a conversion routine)

May we know why you can't implement the solution in that thread, which is obviously the best choice?

Although I'm not fan of adding a column to the ALV, may we know why you can't add it?

I'd like to know, because it will avoid us proposing useless solutions again.

Read only

rosenberg_eitan
Active Contributor
0 Likes
3,540

Hi,

For your knowledge the sorting is done using the internal format of the field .

For demonstration I formatted a date field using FUNCTION 'CONVERSION_EXIT_SDATE_OUTPUT' .

And I get:

Note that the months are sorted based on the actual value.


So it seems that you can format you internal data into a "sortable" format .

Using the exit you can present the internal data to the user as required .

Regards.

Eitan.