core: fix apply of layout when buffers that are not in layout are before some buffers in layout

Now when a layout is applied, the buffers in layout are inserted/sorted
first in the new list. Then the other buffers (not in layout) are added
after this loop, and then they are added after all layout buffers.