On Tue, 22 Oct 2002, michael libby wrote:

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

Kewl! Three things:

Why define your own sort, when you can just pass the comparison method to
normal sort-method? (I may be missing something here, though.)

Second, modules might fit the bill better than classes.

Third, I'd like to suggest some API changes - to make it less perl, and
more ruby-like:

> class Sort
>   class Versions
>     def Versions.versioncmp(version_a, version_b)
>     end
>     def Versions.sort_versions(list)
>     end
>   end
> end

I'd make it:

module Version

  def self.cmp( a, b )
    Sort::Versions::versioncmp( a, b )
  end

  def self.sort( list )
    list.sort { |a,b| Version.cmp(a,b) }
  end

  def self.sort!( list )
    list.sort! { |a,b| Version.cmp(a,b) }
  end

  module Cmp
    def version_cmp( b )
      Version.cmp( self, b )
    end
  end

  module Sort
    def version_sort
      Version.sort( self )
    end
    def version_sort!
      Version.sort!( self )
    end
  end

end

Version.cmp( "0.77", "1.3.5" )

String.extend(Version::Cmp)
"0.1.3".version_cmp( "0.2.32" )

Version.sort(["0.22", "0.1", "1.4"])
Version.sort!(["0.22", "0.1", "1.4"])

Array.extend(Version::Sort)
["0.22", "0.1", "1.4"].version_sort
["0.22", "0.1", "1.4"].version_sort!

This naturally does break the CPAN-equivalence, which may be a nice thing
to have, so maybe make a separate hierarchy: CPAN::Sort::Versions that
refers to the same implementation.

  -- Nikodemus
------------------------------------------------------------
 I refuse to have a battle of wits with an unarmed person.