1834154 Members
2506 Online
110064 Solutions
New Discussion

Sorting strings

 
SOLVED
Go to solution
cam9269
Regular Advisor

Sorting strings

Hiya guys,

Need help with sorting strings. My data looks like this:
dat11
dat1
dat33
dat3
dat10
dat30
dat20
dat31
dat2

the desired output is:

dat1
dat2
dat3
dat10
dat11
dat20
dat30
dat31
dat33

but oviously this would happen:
dat1
dat10
dat11
dat2
dat20
dat3
dat30
dat31
dat33

so, the question is how do i get the desired output done?

TIA!
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: Sorting strings

If your data is always 3 bytes then the number you can use:
sort -k1.1,1.3 -k1.4,1n

If it varies in size, you would have to split the data into two fields then sort and then merge.
But the moral of the story is that you should use leading zeroes with a fixed width for numbers.
cam9269
Regular Advisor

Re: Sorting strings

Wow! great solution,works perfectly fine. This would serve well, because our data will stay this way unless we move out to another system.

And yes, I do agree with the structure, it's just taht i cannot modify what has been implemented and is currently running.

Thanks!