[How to] How can I rearrange the position of small video container in gallery view

I am trying to arrange the gallery view so that participant with same id prefix will be listed on the same line. But after some testing, I found that the arrangement of containers in gallery view seems to be random. Is there any logic on how they are positioned and how I can config it?