in reply to Sorting An Array of Versions

Update: Looking at the other answers, seems I misunderstood the question? Please disregard.

Update 2: tye correctly pointed out that what I was trying to do (failing badly) was this:

sort{ eval "v$a" cmp eval "v$b" } @versions;
which is probably too hacky. :)

----

Since you have "version" strings, you can use the v notation to sort the strings, like this:

sort{ "v$a" cmp "v$b" } @versions;
This may seem a little hacky, but gets the job done.

It vorks like this: v2.0.0 is the same thing as chr(2).chr(0).chr(0), so we can do a stringwise compare, cmp and thus sort these correctly. The result is:

1.0.10 1.0.9 1.1.0 1.1.2 2.0.0
and you can just switch $a and $b to reverse the order.


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
(tye)Re2: Sorting An Array of Versions
by tye (Sage) on Mar 12, 2002 at 19:19 UTC

    "v$a" does not a "vector" make (it just makes "v".$a). You'd have to use something like eval "v$a" for that. Even that won't work in the face of common version number formats like "1.0a".

    Note that "10" is showing as less than "9" in your results.

            - tye (but my friends call me "Tye")
      I noticed. I thought that was what was sought after, and stupidly did not crosscheck against what a direct cmp between the strings did - the same. I did realize my mistake... way too late. Better try to not be so triggerhappy in the future. :)
      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.