Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Numeric Sorting on Characters

by Kenosis (Priest)
on Aug 14, 2013 at 21:16 UTC ( #1049480=note: print w/ replies, xml ) Need Help??


in reply to Numeric Sorting on Characters

If you're using Perl v5.14+, you could use the r (for return the modified string) modifier in a substitution:

use strict; use warnings; my %hash = map { $_ => 1 } qw/987-665-888 456-123-000 000-000-000 123- +456-789/; print "$_\n" for sort { $a =~ s/-//gr <=> $b =~ s/-//gr } keys %hash;

Output:

000-000-000 123-456-789 456-123-000 987-665-888

Hope this helps!


Comment on Re: Numeric Sorting on Characters
Select or Download Code
Re^2: Numeric Sorting on Characters
by AnomalousMonk (Monsignor) on Aug 14, 2013 at 23:22 UTC

    halecommarachel supplies no example data, so this point is a bit unclear, but I assume that the real data has elements that are a  '-' (dash) rather than that contain dashes. E.g.,
        qw(1 10  -  2 -3 20  - -  3 0 2 11 8 7);
    rather than
        qw/987-665-888 456-123-000 000-000-000 123-456-789/

    Caveat Programmor.

      /me nods ...

      In which case, if careful research into the problem confirms that,:

      • Yes, that dash is legitimate data, and
      • there is no bug either in this program or in the upstream source, and
      • the downstream program(s) will know what to do, and therefore,
      • here is how we wish to sort it,

      ... (heh) ...

      Then, as you know, the Perl sort verb relies upon a subroutine to do the sort-comparison, “and this is why.”   Although it is most frequently a “one-liner,” it can be a much more elaborate thing which receives two parameters, $a and $b.   (And, if you do several sorts the same way, it can be a separate, not-inline, subroutine that all of them use.)   All of which is well and good ... but I think that I’m in the majority thinking that a bug .. somewhere upstream .. has been uncovered here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-07-28 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (200 votes), past polls