What order are target selector results returned in if the sort parameter is set to "arbitrary"?
Target selectors are used to select certain entities. They accept different arguments to choose which entity to select. Usually, this returns a list of found entities.
Using the limit=n
parameter limits the length of the list to n
(useful with 1
as n
to get a single entity selection). The sort
parameter lets you choose which order the list of targets are sorted in.
The sort
parameter has 4 options, arbitrary
, furthest
, nearest
, and random
. The Minecraft Wiki displays what each of these do:
sort=nearest
— Sort by increasing distance. (Default for @p, @e,[BE] @a[BE])sort=furthest
— Sort by decreasing distance.sort=random
— Sort randomly. (Default for @r)sort=arbitrary
— Do not sort. (Default for @e,[JE] @a[JE])
nearest
, furthest
, and random
are easy to understand in what they do. The one I am curious about is when the arbitrary
choice is selected.
The item returned by a target selector is always an ordered list. What order are they in if they are not sorted?
Possible Theories
Here are some possible theories:
- The list is sorted by UUID.
- The list is sorted by time of creation. (newest->oldest, or oldest->newest)
- The list is sorted by position of entity. Entities are ordered in a scanline fashion, so lowest Z is first, with X from lowest to highest, followed by those on the next lowest Z.
Solution 1:
It is by definition not guaranteed how they are ordered and Mojang explicitly said (somewhere) that no command contraption should rely on this order. If someone finds this Q&A for any reason other than curiosity, they're probably doing something wrong.
That being said, it seems to have been the order of creation of the entity in memory in all versions from 1.13 to 1.16. This means that if you summon entities and do not close+reopen the world, restart the server, unload and reload the chunks etc., they are pretty much always in summoning order. If you do any of those actions, they are in the order in which their chunks were loaded. Within a chunk, I don't know the order, it might be the same as in the chunk file, which you can inspect with NBTExplorer.