Is the Collection<Entities> returned by the getNearbyEntities(long distance) function sorted in any way?

E.g. closest to farthest?

I don’t believe it is. You can sort it by distance though

How would one do this? Manually using a loop?

First you need to convert the collection into a List. Simplest way is this

List<Entity> entities = new ArrayList<>(collection);

After that you need to use the sort function. And compare the two distances from the original position

Location<World> location;
final Vector3d position = location.getPosition();
list.sort((e1, e2) -> e1.getLocation().getPosition().distance(position) - e2.getLocation().getPosition().distance(position));

That list will now be sorted.

There is also Collections.sort() however the collection needs to be mutable. Which the sponge API does not state if it is, therefore can not be guaranteed on a per implementation bases. Something that works in SpongeCommon many not work the exact same in another implementation such as LanternPowered, however LanternPowered will be in spec

Performance tip: use distanceSquared instead of distance.