Skip to content
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

ArrayComparator ignores order - Part 2 #57

Closed
panvid opened this issue May 30, 2018 · 11 comments
Closed

ArrayComparator ignores order - Part 2 #57

panvid opened this issue May 30, 2018 · 11 comments

Comments

@panvid
Copy link

panvid commented May 30, 2018

As in #44 mentioned the ArrayComparator ignore the order of two arrays, also if the attribute $canonicalize = false is set.

@luckydonald
Copy link

Woops, missed to bump it.

@stale
Copy link

stale bot commented Jul 29, 2018

This issue has been automatically marked as stale because it has not had activity within the last 60 days. It will be closed after 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 29, 2018
@panvid
Copy link
Author

panvid commented Jul 29, 2018

I bump it.

@stale stale bot removed the stale label Jul 29, 2018
@1blankz7
Copy link

1blankz7 commented Sep 7, 2018

There is a test for your use case so I think it is intended.

See:

['a' => 1, 'b' => 2],

@luckydonald
Copy link

Fixed now?

@panvid
Copy link
Author

panvid commented Sep 7, 2018

@sebastianbergmann Is fixed?

@sebastianbergmann
Copy link
Owner

There is nothing to fix here as @1blankz7 explained in #57 (comment).

If you consider two associative arrays that have the same keys and the same values associated with these keys but a different order of elements to be different (I do not, the code does not) then you need to implement a custom comparator and use that.

@luckydonald
Copy link

But isn't the definiton of equal wrong here?
There should be a way to make sure they are actually the same order.

Take for example this completely constructed code:

$html = [
  "h1" => "Greetings",
  "p" => "How are you today?"
]
for ($html as $tag => $content) {
  echo "<$tag>$content</$tag>";
}

One would output:

<h1>Greetings</h1>
<p>How are you today</p>

Greetings

How are you today

But if the equal test ignores order, we might end up with:

<p>How are you today</p>
<h1>Greetings</h1>

How are you today

Greetings


What is the intended way to check that $html is correct here?

@panvid
Copy link
Author

panvid commented Sep 7, 2018

And how should the $canonicalize = false work? This attribute forces to be in correct order.

@luckydonald
Copy link

luckydonald commented Sep 7, 2018

The description says:

/**
 * @param $canonicalize bool  Arrays are sorted before comparison when set to true
 */

This indicates it would check the order. But it doesn't. Which is the problem noted here.

I believe both code and test case are wrong here.

When having $canonicalize = true, ignoring the order makes perfectly sense.

However, if $canonicalize is set to false, It should allow us to really compare the values, including order.

@sebastianbergmann
Copy link
Owner

"Arrays are sorted before comparison when set to true" is meant to (only) apply to non-associative arrays.

SnakeCharm3r pushed a commit to SnakeCharm3r/comparator that referenced this issue Dec 2, 2024
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

4 participants