-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 21 October 2002 22:52, WATANABE Hirofumi wrote:

> How about String#scan ?

That did the trick! This is why I love Ruby, so easy to get stuff done. The 
Ruby version is SO much more readable than the Perl too. 

So here's my adaptation of Perl's Sort::Versions. Anything I'm missing? 
Other than the documentation, that is.

class Sort
  class Versions
    def Versions.versioncmp(version_a, version_b)
      vre = /[-.]|\d+|[^-.\d]+/
      ax = version_a.scan(vre)
      bx = version_b.scan(vre)

      while (ax.length>0 && bx.length>0) do
	a = ax.shift
	b = bx.shift
	
	if( a == b )                 then next
	elsif (a == '-' && b == '-') then next 
	elsif (a == '-')             then return -1
	elsif (b == '-')             then return 1
	elsif (a == '.' && b == '.') then next 
	elsif (a == '.' )            then return -1
	elsif (b == '.' )            then return 1
	elsif (a =~ /^\d+$/ && b =~ /^\d+$/) then
	  if( a =~ /^0/ or b =~ /^0/ ) then
	    return a.to_s.upcase <=> b.to_s.upcase
	  end
	  return a.to_i <=> b.to_i
	else 
	  return a.upcase <=> b.upcase
	end
      end
      return version_a <=> version_b;
    end

    def Versions.sort_versions(list)
      return list.sort{|a,b| Sort::Versions.versioncmp(a,b)}
    end
  end
end

puts Sort::Versions::sort_versions( %w{ 1.1.6 2.3 1.1a 3.0 1.5 1 2.4 1.1-4 
2.3.1 1.2 2.3.0 1.1-3 2.4b 2.4 2.40.2 2.3a.1 3.1 0002 1.1-5 1.1.a 1.06} )
- -- 
<signature>
  <name>Michael C. Libby</name>
  <email>x / ichimunki.com</email>
  <web-site>http://www.ichimunki.com/</web-site>
  <public-key>http://www.ichimunki.com/public_key.txt</public-key>
</signature>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9tM+z4ClW9KMwqnMRAhdYAJsHHLz3+cMTvzCctZqe66zo8W8qKQCffDJl
r1xy27/db+mGDu60gdfLvmM=
=D9xw
-----END PGP SIGNATURE-----