This site requires JavaScript, please enable it in your browser!
Greenfoot back
FlyingRabidUnicornPig
FlyingRabidUnicornPig wrote ...

2013/5/20

How is getObjectsInRange() sorted?

I assume that the closer objects would be but at the beginning, and farther ones last, but I want to verify. It's not on the Greenfoot API.
danpost danpost

2013/5/20

#
You could experiment and figure it out for yourself. Let us know what you find. Remember that location may not be the only factor. It may be in order of creation -- oldest to newest (or newest to oldest; which I doubt, but should still be considered as a possibility).
davmac davmac

2013/5/20

#
It's not on the Greenfoot API.
You've hit on an important aspect of programming here. The documentation for the method doesn't specify any order for the objects, and so it's really wrong to assume a particular order even if you find that there is one. For instance, the objects might be returned in nearest-to-farthest order now, but the implementation of the method might change in the future (it would be a compatible change because the method still matches its description). Because it's unspecified, the order is an implementation detail and is subject to change in future versions; so, if you want a certain order, you should write code that ensures that order (i.e. which sorts the list according to the criteria you want). To answer the question as asked however: there is currently no particular order. (The order is actually determined by the collision checking algorithm, and it doesn't make any attempt to order the list).
Thanks to both of you. I will follow what davmac said and work on making my code work to find the closest object. I've been busy the last 30 minutes writing code to test like danpost said to do (It's been taking a long time because I'm making new objects/interfaces that will be implemented in my final project, and not just for testing). Thanks again.
You need to login to post a reply.