aboutsummaryrefslogtreecommitdiff
path: root/hash.rb
blob: 5a84abe634a492ae6d4f7a7b4fcb3bbb7801f4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
################################
# Author:  Benjamin Kellermann #
# Licence: CC-by-sa 3.0        #
#          see Licence         #
################################

class Hash
	def compare_by_values(other, fieldarray)
		return  0 if fieldarray.size == 0
		return  1 if  self[fieldarray[0]].nil?
		return -1 if other[fieldarray[0]].nil?

		if self[fieldarray[0]] == other[fieldarray[0]]
			if fieldarray.size == 1
				return 0
			else
				return self.compare_by_values(other, fieldarray[1,fieldarray.size-1])
			end
		else
			self[fieldarray[0]] <=> other[fieldarray[0]]
		end
	end
end

if __FILE__ == $0
require "test/unit"
  class Hash_test < Test::Unit::TestCase
    def test_compare_by_values
			a = {1 =>1, 2=>2, 3=>3}
			b = {1 =>1, 2=>2, 3=>2}
			c = {1 =>1, 2=>3, 3=>3}
			d = {1 =>1, 2=>3}

      assert_equal( 0,a.compare_by_values(a,[1,2,3]))
      assert_equal( 1,a.compare_by_values(b,[1,2,3]))
      assert_equal(-1,a.compare_by_values(c,[1,2,3]))
      assert_equal( 1,c.compare_by_values(d,[1,2,3]))
      assert_equal(-1,d.compare_by_values(c,[1,2,3]))
      assert_equal( 0,d.compare_by_values(c,[]))
    end
  end 
end