E
E
eldar_web2018-08-03 14:41:03
ruby
eldar_web, 2018-08-03 14:41:03

How can two arrays be compared in Ruby if the order is different?

For example, there is an array:

foo1 = [
{name: 'Траволта', age: '15'},
{name: 'Чубайс', age: '50'}
]

foo2 = [
{ name: 'Чубайс', age: '50'},
{name: 'Траволта', age: '15'}
]

Their content is the same, but their order is different,
and if equal, == will return false. How can you do an equality check in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Blokhin, 2018-08-03
@TITnet

You have different arrays.
[0, 1] != [1, 0].
Try to compare like this.

foo1.sort_by { |h| h.first } == foo2.sort_by { |h| h.first }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question