If by "sort a vec" you mean, treat a bitstring as a collection of n-bit values and then sort them using perl's built in sort function without converting it/them to an array, you don't. At least, not easily.

The input to sort is an array as is the return. if you bit-fields are of equal length, which is also a power of 2, then you can use vec (or unpack/pack with a pattern of '(bn)*' to convert them to/from an array.

vec( $vec, $_, 4) = rand(16) for 0 .. 99; print map{ vec( $vec, $_, 4)} 0 .. 99; 6 15 0 10 4 6 10 1 13 9 7 1 1 1 7 12 3 6 14 7 4 7 8 7 0 4 10 0 13 6 6 9 14 7 10 5 12 4 15 11 13 12 7 2 13 14 4 7 0 10 9 4 10 5 3 12 10 9 15 12 5 3 8 0 0 6 7 0 2 14 14 1 3 13 4 15 8 10 2 8 11 2 0 14 10 4 8 6 10 1 10 7 10 13 6 5 12 6 0 15 $p=0; map{ vec( $vec, $p++, 4) = $_ } sort{ $a <=> $b } map{ vec( $vec, $_, 4) } 0 .. 99; print map{ vec( $vec, $_, 4)} 0 .. 99; 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 11 11 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15

It would be possible supply sort with a list of integers 0 to the length (in bitfields) of your vector and to play games inside the sort block, performing the switching of the bit-strings yourself using vec and returning the approriate value (-1, 0, 1) yourself to indicate what you had done. This is difficult to get right, fragile as heck, and grossly inefficient as the convertion for testing would be done for every comparison of every field, rather than just once at the beginning and once at the end. Nothing more than an intellectual curiosity. I probably shouldn't have even mentioned it:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!


In reply to Re: sorting a vec by BrowserUk
in thread sorting a vec by mcinnes

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.