You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Original saves are [0, 1, 2, 3, 4]# This version results in obj.SaveGames of [4, 3, 3, 4, 0]obj.SaveGames=sorted(obj.SaveGames,
key=lambdax: x.LastSaveDate,
reverse=True)
Slightly more insidiously, this is also equivalent to obj.SaveGames.sort(key=lambda x: x.LastSaveDate, reverse=True), which you might expect to work better.
The root cause is structs being reference types, so when we update the first value in the array we're changing the value of whatever index it got sorted to. This is very unintuitive.
As a workaround, you can make a copy of each struct before assigning them:
Original report was
Slightly more insidiously, this is also equivalent to
obj.SaveGames.sort(key=lambda x: x.LastSaveDate, reverse=True)
, which you might expect to work better.The root cause is structs being reference types, so when we update the first value in the array we're changing the value of whatever index it got sorted to. This is very unintuitive.
As a workaround, you can make a copy of each struct before assigning them:
This is a broader problem than just sorting though, any slice assignment might run into it
And then more generally, 2d arrays or arrays of multicast delegates, currently the other two reference types, would also be vulnerable.
The text was updated successfully, but these errors were encountered: