Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Re: Dictionary-style sort a la Tcl?

by argathin (Acolyte)
on Apr 18, 2002 at 12:56 UTC ( #160186=note: print w/replies, xml ) Need Help??

in reply to Re: Dictionary-style sort a la Tcl?
in thread Dictionary-style sort a la Tcl?

First of all: A big THANKS to all who responded! The solutions offered by ariels and Juerd seem to come the closest to what I need (BTW: Thanks for pointing out that I need to rethink my requirements!). blakem's solution probably won't work, as I can't make the assumption mentioned by him about the data I get and moodster's solution doesn't seem to work for all cases (though I didn't quite find out why).
In any case, I learned a few more things, which is good... :-)

As for Juerd's code:
That's a Schwartzian Transform, right? (Didn't know it existed until today... :-})
However, there seems to be one problem: I get quite a few of Argument "" isn't numeric in numeric comparison (<=>) ... if any of the strings starts with a number. If I understand your code correctly, the problem lies in the : $A <=> $B part. When the string starts with a number, the split generates a first field that's defined, but empty. After replacing $A =~ /\D/ || $B =~ /\D/ with $A !~ /\d+/ || $B !~ /\d+/ (is there a better way?), it worked.

There's also a second problem, but that's partly due to my apparently incomplete requirements - I wasn't aware of that until I experimented a bit with the solutions offered here. Take the following data set:

Unsorted:x10y 1abc a10y x9y b1 abc DEF 123DEF bigboys big9boys 123DEFG 123def

Using your code or ariels' code, the result is:

Sorted:1abc 123DEF 123def 123DEFG a10y abc b1 big9boys bigboys DEF x9y x10y

Whether "big9boys" should appear before "bigboys" is probably debatable - I'm not 100% sure myself wich way round would be better in my case...
I am, however, wondering about "1abc" coming before "123DEF" (and the other "123..."), though I can't see a way of changing that without breaking the other requirements. I'll have to think about it.

Thanks again,

Replies are listed 'Best First'.
Re: Re: Re: Dictionary-style sort a la Tcl?
by Juerd (Abbot) on Apr 18, 2002 at 15:22 UTC

    hat's a Schwartzian Transform, right?

    It is. In short: by creating a datastructure that you store together with the original string, you don't have to re-build the original string and don't have to take apart the string at every iteration.

    After replacing $A =~ /\D/ || $B =~ /\D/ with $A !~ /\d+/ || $B !~ /\d+/ (is there a better way?), it worked.

    Your correction was correct. You can actually even drop the plusses. I'll update my code right away.

    I am, however, wondering about "1abc" coming before "123DEF" (and the other "123...")

    As I see it, that is correct. Integers should be used as such, according to your definition. That was the challenge :) First, the string is split to integer and non-integer parts, and then they are compared to eachother.

    out of (1, abc) and (123, DEF), the first wins because 1 < 123.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.

Re: Re: Re: Dictionary-style sort a la Tcl?
by riffraff (Pilgrim) on Apr 18, 2002 at 13:53 UTC
    Actually, I think that would be correct. 1 would come before 123 in numeric sequence (according to your definition of the -dictionary sort). Also, I think big9boys would appear before bigboys, but just because in ascii order the digits come before letters.
      Yes, you are right, of course - it just looked strange on first glance. However, after I've reviewed my requirements, I don't think it'll cause any problems either way, so the solutions offered here are perfect. :-)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://160186]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-01-23 10:17 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (243 votes). Check out past polls.