tags:

views:

35

answers:

2

I am having two structures similar to the one below.

[1 => [{'pc'=>1,'id'=>0},{'pc'=>4,'id'=>0},{'pc'=>2,'id'=>1]]

But both of them need not contain the inside array in the exact same order. How to compare in such case?

+1  A: 

If the order isn't important, you should consider other structures instead of Array there, Set, for example.

You can use Set for comparing as well, by converting Arrays to Sets before comparison:

require 'set'
a = [{:a => 2}, {:b => 3}]
b = [{:b => 3}, {:a => 2}]

sa = Set.new a
#=> #<Set: {{:a=>2}, {:b=>3}}>
sb = Set.new b
#=> #<Set: {{:b=>3}, {:a=>2}}>

a == b
#=> false
sa == sb
#=> true
Mladen Jablanović
A: 

It seems a simple compare works:

data = {
  1 => [{'pc'=>1,'id'=>0},{'pc'=>4,'id'=>0},{'pc'=>2,'id'=>1}],
  2 => [{'pc'=>1,'id'=>0},{'pc'=>4,'id'=>0},{'pc'=>2,'id'=>1}],
  3 => [{'pc'=>1,'id'=>0},{'pc'=>2,'id'=>1},{'pc'=>4,'id'=>0}]
}
data[1] == data[2]
#> true
data[2] == data[3]
#> false
Marcus Derencius