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

Sorting on macOS and Ubuntu acts differently #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

duleorlovic
Copy link

I had a problem to run same code on different machines since the sort was giving different results for the array of rules with same properties.
Now we use sort_by.with_index so the order is preserved

On different computers sort behaves differently for array of objects
with equal properties. This way we preserve original order of rules
@duleorlovic
Copy link
Author

To see how sorting object with same property behaves differently on macOS and linux you can run:

# on linux the order preserved
8.times.map {|i| {i: i, p: 0}}.sort_by {|h| h[:p]}.map {|h| h[:i]}
 => [0, 1, 2, 3, 4, 5, 6, 7] 

# on mac the order is not original order
8.times.map {|i| {i: i, p: 0}}.sort_by {|h| h[:p]}.map {|h| h[:i]}
 => [7, 1, 2, 3, 4, 5, 6, 0] 

# on mac for small arrays works stable
7.times.map {|i| {i: i, p: 0}}.sort_by {|h| h[:p]}.map {|h| h[:i]}
 => [0, 1, 2, 3, 4, 5, 6] 

That is the reason why I update lib/core/engine.rb to

agenda.sort_by.with_index { |a, i| [a, i] }

so we have original order preserved

8.times.map {|i| {i: i, p: 0}}.sort_by.with_index {|h, n| [h[:p], n]}.map {|h| h[:i]}
 => [0, 1, 2, 3, 4, 5, 6, 7] 

and engines rules are performed in the same order on both mac and linux

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

Successfully merging this pull request may close these issues.

None yet

1 participant