Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Sorting strings numerically Post Reply Post New Topic
Author Message
crystalnewbie33
Newbie
Newbie


Joined: 08 Jan 2014
Online Status: Offline
Posts: 29
Quote crystalnewbie33 Replybullet Topic: Sorting strings numerically
    Posted: 27 Oct 2014 at 5:39am
I have a string field that needs to be sorted numerically instead of alpha-numeric. Only numbers will be entered into the field. Alpha-numeric shows this way:
1
10
11
2
23
37
8
I found a formula that works by creating a formula field with the following:
Right("0000" & {table.field},4)
However, I don't particularly like this solution as it results in this:
0001
0002
0008
0010
0011
0023
0037
I prefer to not have the zeros. Is there a way I can hide the zeros or is there another solution for sorting string fields numerically that would work better (and hopefully be just as simple)?
IP IP Logged
crystalnewbie33
Newbie
Newbie


Joined: 08 Jan 2014
Online Status: Offline
Posts: 29
Quote crystalnewbie33 Replybullet Posted: 27 Oct 2014 at 5:55am
I tried
ToNumber(Right("0000" & {table.field},4))
and it seems to work. Are there any concerns with using this formula?
IP IP Logged
z9962
Senior Member
Senior Member
Avatar

Joined: 04 Jul 2012
Online Status: Offline
Posts: 161
Quote z9962 Replybullet Posted: 27 Oct 2014 at 6:11am
why not just use and sort on this?
ToNumber({table.field})
IP IP Logged
crystalnewbie33
Newbie
Newbie


Joined: 08 Jan 2014
Online Status: Offline
Posts: 29
Quote crystalnewbie33 Replybullet Posted: 27 Oct 2014 at 6:27am
Thank you for the suggestion. That works just as well and is a simpler formula. What if the user enters a letter into the field? They should not do it intentionally but is possible. ToNumber will return an error. I guess that is why the original formula I used was suggested to me. Do you know how I could hide the zeros using my original formula?
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 27 Oct 2014 at 6:47am
your original formula would also fail on a non numeric value as it is also using the tonumber() function.
You have to decied what you want done with a non-numeric value. If you want to force tehm to the top of the sort then somethine like
 
if isnumeric({table.field}) then ToNumber({table.field})  else -1
 
You can use that for sorting (in the sort expert) and then use the original field for display on the report canvas.
if it is used in a grouping then suppress the group field and drop the original field on the group header for display


Edited by DBlank - 27 Oct 2014 at 8:19am
IP IP Logged
crystalnewbie33
Newbie
Newbie


Joined: 08 Jan 2014
Online Status: Offline
Posts: 29
Quote crystalnewbie33 Replybullet Posted: 27 Oct 2014 at 8:08am
Thanks DBlank, I didn't think to sort by the converted field but simply display the original field. That was the palm to the forehead I needed.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.