Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: sorting number

by kcott (Archbishop)
on Apr 09, 2019 at 07:47 UTC ( [id://1232337]=note: print w/replies, xml ) Need Help??


in reply to sorting number

G'day dideod.yang,

What you have as "I want to print ..." is not really sorted. Perhaps you wrote COREA12 and COREA115 around the wrong way. If that's the case, you'd need to split each element into alphabetic and numeric parts but do a string comparison on both of them:

$ perl -E ' say map "$_->[0]$_->[1] ", sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } map [/(\D+)(\d+)/], qw{ CORE1 CORE12 CORE8 CORE233 COREA11 COREA12 COREA130 COREA115 } ' CORE1 CORE12 CORE233 CORE8 COREA11 COREA115 COREA12 COREA130

— Ken

Replies are listed 'Best First'.
Re^2: sorting number
by Eily (Monsignor) on Apr 09, 2019 at 07:58 UTC

    My guess is that it was the CORE8 that was misplaced, and GrandFather's and Tux's solutions are the expected output.

    I suppose you meant to write <=> on the right side? Otherwise your code gives the same output as a plain sort

      "My guess is that it was the CORE8 that was misplaced, ..."

      Quite possibly - it's all guesswork. :-)

      "I suppose you meant to write <=> on the right side?"

      No, cmp was intended. I was aiming to indicate that cmp would give "11 115 12 130"; whereas <=> would give "11 12 115 130".

      It would appear I failed to communicate my intent. Thanks for pointing this out and giving me the opportunity to provide clarification.

      — Ken

Re^2: sorting number
by AnomalousMonk (Archbishop) on Apr 09, 2019 at 08:00 UTC
    Perhaps you wrote COREA12 and COREA115 around the wrong way.

    But then the desired sort order just becomes the default lexi sort:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @sorted = sort qw(CORE1 CORE12 CORE8 CORE233 COREA11 COREA12 COREA130 COREA115) ; print qq{@sorted}; " CORE1 CORE12 CORE233 CORE8 COREA11 COREA115 COREA12 COREA130


    Give a man a fish:  <%-{-{-{-<

      See my response to ++Eily's post - he asked about the same thing.

      — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232337]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found