class Naturalsorter::Sorter
Public Class Methods
Source
# File lib/naturalsorter.rb, line 74 def self.bigger?(a, b) return false if a.eql?( b ) return false if Versioncmp.compare(a, b) == 0 newest = self.get_newest a, b newest.eql?( a ) end
Source
# File lib/naturalsorter.rb, line 88 def self.bigger_or_equal?(a, b) return true if a.eql?(b) return true if Versioncmp.compare(a, b) == 0 newest = self.get_newest a, b newest.eql?(a) end
Source
# File lib/naturalsorter.rb, line 69 def self.get_newest(a, b) Versioncmp.replace_leading_vs a, b Sorter.get_newest_version(a, b) end
Source
# File lib/naturalsorter.rb, line 63 def self.get_newest_version(first, second) array = [first, second] array = array.sort { |a,b| Versioncmp.compare( a, b ) } array.last end
Source
# File lib/naturalsorter.rb, line 107 def self.is_version_current?(version, newest_version) version = version.gsub("~>", "") version = version.gsub(" " , "") versions = version.split(".") newests = newest_version.split(".") min_length = versions.size if newests.size < min_length min_length = newests.size end min_length = min_length - 2 if min_length < 0 min_length = 0 end (0..min_length).each do |z| ver = versions[z] cur = newests[z] if cur > ver return false end end if newests.size < versions.size ver = versions[min_length + 1] cur = newests[min_length + 1] if cur > ver return false end end true end
This is for the GEM notaiton ~> For example ~>1.1 fits 1.2 and 1.9 and 1.14 But not 2.0
Source
# File lib/naturalsorter.rb, line 137 def self.replace_minimum_stability val VersionTagRecognizer.remove_minimum_stability val end
Source
# File lib/naturalsorter.rb, line 81 def self.smaller?(a, b) return false if b.eql?( a ) return false if Versioncmp.compare(a, b) == 0 newest = self.get_newest a, b newest.eql?( b ) end
Source
# File lib/naturalsorter.rb, line 95 def self.smaller_or_equal?(a, b) return true if a.eql?(b) return true if Versioncmp.compare(a, b) == 0 newest = self.get_newest a, b newest.eql?(b) end
Source
# File lib/naturalsorter.rb, line 32 def self.sort(array, caseinsensitive = false , asc = true ) return array if (array.nil? || array.empty?) return array.sort { |a, b| Natcmp.natcmp(a, b, caseinsensitive) } if asc return array.sort { |a, b| Natcmp.natcmp(b, a, caseinsensitive) } end
Source
# File lib/naturalsorter.rb, line 39 def self.sort_by_method(array, method, caseinsensitive = false, asc = true) return array if (array.nil? || array.empty? || array.length == 1) return array.sort { |a, b| Natcmp.natcmp( a.send(method), b.send(method), caseinsensitive) } if asc return array.sort { |a, b| Natcmp.natcmp(b.send(method), a.send(method), caseinsensitive) } end
‘Natural order’ sort for an array of objects.
Source
# File lib/naturalsorter.rb, line 46 def self.sort_version(array, asc = true) return array if (array.nil? || array.empty? || array.length == 1) return array if (array[0].nil? || array[1].nil?) return array.sort { |a,b| Versioncmp.compare( a, b ) } if asc return array.sort { |a,b| Versioncmp.compare( b, a ) } end
Source
# File lib/naturalsorter.rb, line 54 def self.sort_version_by_method( array, method, asc = true ) return array if (array.nil? || array.empty? || array.length < 1 || method.to_s.strip.empty? ) return array if (array[0].nil? || array[1].nil?) return array if (array[0].send(method).nil? || array[1].send(method).nil?) return array&.sort { |a,b| Versioncmp.compare(a.send(method), b.send(method)) } if asc return array&.sort { |a,b| Versioncmp.compare(b.send(method), a.send(method)) } end