diff --git a/lib/daru/helpers/array.rb b/lib/daru/helpers/array.rb index f9cbefe1f..8fa1b3888 100644 --- a/lib/daru/helpers/array.rb +++ b/lib/daru/helpers/array.rb @@ -36,5 +36,18 @@ def array_of?(array, match) !array.empty? && array.all? { |el| match === el } # rubocop:disable Style/CaseEquality end + + def sort_composite_data(array) + array.sort + rescue ArgumentError, TypeError => msg + case msg.to_s + when /comparison of Symbol with String failed/, + /comparison of Symbol with \d+ failed/, + /comparison of String with :.* failed/, + /comparison of Integer with :.* failed/, + /no implicit conversion from nil to integer/ + return array.sort_by(&:to_s) + end + end end end diff --git a/lib/daru/maths/arithmetic/vector.rb b/lib/daru/maths/arithmetic/vector.rb index e914903e1..5777b72ea 100644 --- a/lib/daru/maths/arithmetic/vector.rb +++ b/lib/daru/maths/arithmetic/vector.rb @@ -92,7 +92,7 @@ def v2o_binary operation, other def v2v_binary operation, other, opts={} # FIXME: why the sorting?.. - zverok, 2016-05-18 - index = (@index.to_a | other.index.to_a).sort + index = ArrayHelper.sort_composite_data(@index.to_a | other.index.to_a) elements = index.map do |idx| this = self.index.include?(idx) ? self[idx] : nil diff --git a/spec/maths/arithmetic/vector_spec.rb b/spec/maths/arithmetic/vector_spec.rb index 2d3289db6..cdea53e29 100644 --- a/spec/maths/arithmetic/vector_spec.rb +++ b/spec/maths/arithmetic/vector_spec.rb @@ -24,7 +24,7 @@ end it "appropriately adds vectors with numeric and non-numeric indexes" do - pending "Need an alternate index implementation?" + # pending "Need an alternate index implementation?" v1 = Daru::Vector.new([1,2,3]) v2 = Daru::Vector.new([1,2,3], index: [:a,:b,:c])