Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

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

Let's start with a re-phrase. A software version consists of three numeric parts, plus any number of additional characters. The three numeric bits are sorted in numeric order, and the character bits in character order.

Here's one way to do it: Convert the human-readable version number into a cannonical form that's easy to compare. Then sort. Then convert them back into human-readable version numbers. A first attempt looks like this:

sub cannonify { my $ver = shift; $ver =~ /(\d+)\.(\d+)\.(\d+)(\w*)/ return chr($1).chr($2).chr($3).$4; } sub uncannonfiy { my $ver = shift; return ord(substr($ver, 0, 1)) . '.' . ord(substr($ver, 1, 1)) . '.' . ord(substr($ver, 2, 1)) . . substr($ver, 3); }

You will note that this isn't very efficent. Try combining it with the Schwartzian transform that other people have pointed you out, and without specifiying a comparator function to your sort call (to sort lexographicly).

Also, if you just want to find the maximum version, don't use sort, which has to do much more work, instead, use List::Util's max().


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).


In reply to Re: Sorting non-standard elements by theorbtwo
in thread Sorting non-standard elements by tradez

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 learning in the Monastery: (5)
As of 2024-03-28 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found