Discord
Login
Community
DARK THEME

reporting a sortList bug

Avatar is a class This class contains x,y coordinate which i define here in array avatars:

avatars=[] avatars.insert(new Avatar(50,-50)) avatars.insert(new Avatar(-10,-40)) avatars.insert(new Avatar(-20,-30))

I tried to sort it with calling

avatars.sortList(function(a1,a2) return a1.y<a2.y end)

And when i print the sorted array the values are: -30,-40,-50

for a in avatars print(a.y) end

When i turn around the condition in sortList to return a1.y>a2.y then the result of sort is again : -30,-40,-50

The function passed to sortList must return a difference, that is to say:

  • a negative value if a1 must be sorted before a2
  • a positive value if a1 must be sorted after a2
  • zero whenever a1 and a2 must be considered equal

Thus to correctly order the list according to the value of y:

avatars.sortList( function(a1,a2) return a1.y - a2.y end )

Thanks for the correction. It works now. :)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community