Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Though there are ways to do it in a single pass, I found it a bit easier to think about if one first splits the list out into two and then sorts them individually. Note I'm using Sort::Key::Natural to sort the mixed values in a hopefully intuitive way (Update: you may note I added the test case "12c" to demonstrate the difference to a simple sort).

use warnings; use strict; use Test::More; use List::Util qw/uniq/; use Sort::Key::Natural qw/natsort/; my @in = ( "2bc", "1", "12c", "12", "21", "1", "1a", "2", "2", "2", "2", "3", "35", "31", "2b", "4", "42", "5", "51", "2ac", "52", "6", "7" ); my @expect = ( "1", "2", "3", "4", "5", "6", "7", "12", "21", "31", "35","42", "51", "52", "1a", "2ac", "2b", "2bc", "12c" ); my (@nums, @mixed); /^\d+$/ and push @nums, $_ or push @mixed, $_ for @in; my @out = uniq( sort( { $a <=> $b } @nums), natsort(@mixed), ); is_deeply \@out, \@expect or diag explain \@out; done_testing;

As opposed to your previous thread, these tags look much more like HTML/XML, so you really should use an appropriate parser!


In reply to Re: Sorting numerials first and then numerials with alpha characters last by haukex
in thread Sorting numerials first and then numerials with alpha characters last by VladP

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-29 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found