Skip to content

Sorting on macOS and Ubuntu acts differently#25

Open
duleorlovic wants to merge 2 commits intoRuleby:masterfrom
duleorlovic:master
Open

Sorting on macOS and Ubuntu acts differently#25
duleorlovic wants to merge 2 commits intoRuleby:masterfrom
duleorlovic:master

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.

1 participant