Why are galaxies much closer spaced (relative to their size) than stars? I have read this question:

So the average spacing is somewhere in the range of 10 - 100 times the size of the biggest galaxies. The peas I had for lunch today were (at a guess - I didn't measure them!) 5mm in diameter so the interpea spacing would be 5 - 50cm, or between 8 and 8,000 per cubic metre.
However there is a take home message i.e. galaxies are much, much, much closer relative to their size than stars are.

Now this is a very nice description, but there is no explanation as to why this should be so. As far as I understand, both structures (galaxies of stars and clusters and superclusters of galaxies themselves) are governed by the same gravitational laws of our universe.
Naively I would think it should be the other way around, dark energy should make things fly apart faster if they are farther apart, and it should have been doing so for billions of years. Galaxies, and especially the intergalactic voids should (as fas as I understand) cause them (the galaxies) to be separated by far bigger relative distances (please note we are talking about distances relative to the objects' size) then the stars are separated inside them.
There are two things to consider:

*

*galaxies are giant objects relative to the size of a star, that is understandable


*but gravity holds stars inside galaxies closer, and has been doing so for billions of years, whilst dark energy has been making galaxies themselves fly apart for billions of years, at an accelerated rate
Though I cannot find any kind of explanation as to why this should be the way it is, that is, why are galaxies (relative to their size) much closer spaced the the stars inside them?
Question:

*

*Why are galaxies much closer spaced (relative to their size) than stars?

 A: I think the answer is not in the spacing, it is in their relative sizes and their constituents. Galaxies and (proto)stars begin their lives with very similar separations compared with their sizes, but protostars become much smaller, while galaxies remain roughly the same size.
A typical stellar density in the galactic disc is 0.1 pc$^{-3}$, so an average spacing between stars is the inverse cube root of this, $\sim 2$ pc, or $\sim 10^8$ solar radii. The distance from the Milky Way to Andromeda is $\sim 7\times 10^5$ pc, which is only $10-100$ times the "radius" of either of them (depending on exactly how you define the radius).
However, stars begin their lives as protostellar clouds that are much larger than the final stars they produce.  Protostellar "cores" are measured to have sizes of 0.01-0.1 pc, with an average size of 0.04 pc (Zhang et al. (2018). Thus at this early stage in their lives, the separation of stars relative to their sizes is quite similar to that of galaxies.
The reason for the subsequent difference is how those systems evolve. The protostar is made up of atoms and molecules that are able to interact and radiate away their internal kinetic energy. This removes internal pressure support, allowing the core to collapse. Ultimately, this collapse is only halted (or perhaps we should say paused) by the onset of nuclear reactions, and it is this that sets the size scale of a star and thus the relative separation of stars in terms of their radii.
Galaxies are made up of stars but also dark matter, the latter dominating in most cases. Stars are essentially point-like, collisionless particles in a galaxy, so neither they or the dark matter are capable of radiating away the galaxy's internal kinetic energy. Thus once galaxies have become relatively gas-poor, by forming lots of stars then they become incapable of getting smaller; further collapse is prevented by their internal kinetic energy, which cannot be dissipated.
Thus whilst stars become orders of magnitude smaller than when they begin to form, galaxies stay at more-or-less the same size.
