# Array equality question

**URL:** <https://rubytalk.org/t/array-equality-question/34703>\
**Category:** ruby-talk\
**Created:** [22 January 2007 22:30 UTC](https://rubytalk.org/t/array-equality-question/34703 "2007-01-22T22:30:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_McMahon](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@Chris\_McMahon](https://rubytalk.org/u/Chris_McMahon)\
**Post date:** [22 January 2007 22:30 UTC](https://rubytalk.org/t/array-equality-question/34703/1 "2007-01-22T22:30:07Z")

</div>

This took me by surprise:

irb(main):001:0\> a1 = ['x','y','z']  
=\> ["x", "y", "z"]  
irb(main):002:0\> a2 = ['y','z','x']  
=\> ["y", "z", "x"]

irb(main):008:0\> puts "yo" if a1 == a2  
=\> nil  
irb(main):009:0\> puts "yo" if a1 != a2  
yo  
=\> nil

I assumed order of elements would not be considered for equality. Is  
there any particular reason for this behavior?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 January 2007 22:40 UTC](https://rubytalk.org/t/array-equality-question/34703/2 "2007-01-22T22:40:06Z")

</div>

Why should order not matter? An array is an \*ordered\* list of elements as indexing by position indicates. a1[0] == a2[0] will return false, why then should a1 == a2 return true?

If you to ignore order you either have to compare sorted copies or resort to Set.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 22.01.2007 23:27, Chris McMahon wrote:
> 
> > This took me by surprise:
> > 
> > irb(main):001:0\> a1 = ['x','y','z']  
> > =\> ["x", "y", "z"]  
> > irb(main):002:0\> a2 = ['y','z','x']  
> > =\> ["y", "z", "x"]
> > 
> > irb(main):008:0\> puts "yo" if a1 == a2  
> > =\> nil  
> > irb(main):009:0\> puts "yo" if a1 != a2  
> > yo  
> > =\> nil
> > 
> > I assumed order of elements would not be considered for equality. Is  
> > there any particular reason for this behavior?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [22 January 2007 22:45 UTC](https://rubytalk.org/t/array-equality-question/34703/3 "2007-01-22T22:45:08Z")

</div>

Chris McMahon wrote:

> This took me by surprise:
> 
> irb(main):001:0\> a1 = ['x','y','z']  
> =\> ["x", "y", "z"]  
> irb(main):002:0\> a2 = ['y','z','x']  
> =\> ["y", "z", "x"]
> 
> irb(main):008:0\> puts "yo" if a1 == a2  
> =\> nil  
> irb(main):009:0\> puts "yo" if a1 != a2  
> yo  
> =\> nil
> 
> I assumed order of elements would not be considered for equality. Is  
> there any particular reason for this behavior?

The reason for this behavior is that the creator of Ruby  
and most of its users aren't devoid of knowledge of  
programming. An array is not a set. An array is not a  
hash (a.k.a. associative array). So of course order matters.  
Does the order in which you read the chapters of a novel  
matter? If order doesn't matter, then why is there a 'sort'  
method that changes the order of an array?

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [22 January 2007 22:45 UTC](https://rubytalk.org/t/array-equality-question/34703/4 "2007-01-22T22:45:52Z")

</div>

The concept of order is an integral part of what it means to be an Array.  
If you aren't interested in order, then a Set is probably a better  
structure.

require 'set'

a = [1,2,3]  
b = a.reverse  
p a == b # false  
p Set.new(a) == Set.new(b) # true

Gary Wright

> **···**
>
> On Jan 22, 2007, at 5:30 PM, Chris McMahon wrote:
> 
> > I assumed order of elements would not be considered for equality. Is  
> > there any particular reason for this behavior?
