Application Development 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: 

How to sort a character field with numbers and letters on the end

kmoore007
Active Contributor
0 Kudos

I have an internal table with character field which has data like this after I sort it. This is not what I expect from the sort.:

13A

15A

29A

30A

31A

33A

123A

125

62

76

94A

I expect this, which is sorted number first, then alpha. Any ideas how to get this sort?:

13A

15A

29A

30A

31A

33A

62

76

94A

123A

125

1 ACCEPTED SOLUTION

brad_bohn
Active Contributor
0 Kudos

That isn't the order I would expect either, nor is the order you specified. What operating system and codepage is it? The 123A and 125 values should be at the top of the list in an ASCII based sort. If you use Rob's suggestion, you'll have to use an integer or other number type field for the number side; a CHAR field will still bubble the 123 and 125 values to the top in ASCII.

6 REPLIES 6

Former Member
0 Kudos

Split into two fields and sort them individually.

Rob

brad_bohn
Active Contributor
0 Kudos

That isn't the order I would expect either, nor is the order you specified. What operating system and codepage is it? The 123A and 125 values should be at the top of the list in an ASCII based sort. If you use Rob's suggestion, you'll have to use an integer or other number type field for the number side; a CHAR field will still bubble the 123 and 125 values to the top in ASCII.

kmoore007
Active Contributor
0 Kudos

We are on Windows NT, but I'm doing this sort in ABAP in an internal table, so not sure why the operating system would effect it.

0 Kudos

Excel sorting results into this:


123A
125 
13A 
15A 
29A 
30A 
31A 
33A 
62 
76 
94A 

As Rob has suggested, you need to have your own algorithm for sorting as your per your requirement.

Regards,

Naimesh Patel

Former Member
0 Kudos

Different operating systems can have different collating sequences (and I think in some you can specify your own).

But this does look wierd. I tried sorting it and got Brad's sequence, not yours (Linux).

@Naimesh - actually I got your sequence.

Rob

Edited by: Rob Burbank on May 26, 2011 4:36 PM

kmoore007
Active Contributor
0 Kudos

Thanks for the suggestions. After reading the suggestions and reviewing some other post threads, here's what I did to get the sort to work:

Add a dash - to the end of any record that did not have an alphabet character at the end. This way, all records have at least one non-numeric character at the end. Then I overlayed zeros '0000000' over each record. Then I sorted the table. Afterwards, I removed the dashes and zeros.

The long way around, but it worked for me. Since the internal table is processed in memory, it doesn't add much overhead to the run time.