Skip to content

Two arrays with same elements but different orders should be considered as equal #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue May 22, 2015 · 6 comments

Comments

@ghost
Copy link

ghost commented May 22, 2015

equal([1,2,3], [1,2,3]) -> true

equal([1,2,3], [3,2,1]) -> false

How to get the true from the second statement?

@xgbuils
Copy link

xgbuils commented Oct 18, 2015

Hi @kitz17

I don't understand why two arrays with same elements but different orders should be considered as equal.

You can get true if uses:

equal([1,2,3].sort(), [3,2,1].sort())

@MrBenJ
Copy link

MrBenJ commented Nov 29, 2016

I agree with @xgbuils - in regular old JS, the order of elements inside an Array matters. @ghost - If you're trying to evaluate if 2 Arrays have the same stuff, but don't care for the order, maybe using a different data type like Map, WeakMap, Set, or WeakSet would be better for you.

@jmakeig
Copy link

jmakeig commented Apr 4, 2017

If you don’t care about order or repeats, you should turn your Array into a Set. However, then you run into #46.

@wilfredjonathanjames
Copy link

Order is a first-class property of arrays. Two arrays of varying order whose contents are the same will behave differently. They are unequal.

If you need this behaviour, Array.prototype.sort both arrays. If they contain the same elements they will be deeply equal.

@Lazarencjusz
Copy link

Lazarencjusz commented Nov 14, 2017

It does not solve this problem:
equal([ {keyA:'a', keyB: 'b' }, {keyD:'c', keyD: 'd' } ], [{keyA:'a', keyB: 'b' }, {keyD: 'd', keyD:'c' }] )

@ljharb
Copy link
Member

ljharb commented Jul 29, 2019

If you want order not to matter, sort your arrays first.

@ljharb ljharb closed this as completed Jul 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants