Sorting manually using layout

Hello, I can’t manage to sort my files according to their position in the layout in the panel. It keeps getting back at their name sorting.
Many thanks for the help,
best



<div class="gallery">
            <?php foreach ($page->files() as $file): ?>
                <?php $file->sortBy('sort') ?>
                <?php if($file->type() == 'image'): ?>
                    <div><figure><img src="<?= $file->url() ?>" alt=""></figure></div>
                <?php endif ?>
                <?php if($file->type() == 'video'): ?>
                    <video width="auto" height="fit-content" controls controlslist="nodownload, noremoteplayback" disablePictureInPicture
                        src="<?= $file->url() ?>">
                    </video>
                <?php endif ?>
            <?php endforeach ?>
layout:
        type: fields
        fields:
          Gallery:
            type: layout
            layouts:
              - "1/3, 1/3, 1/3"
            fieldsets:
              - image
              - video

Hey, the first problem is that you are not sorting your files at all, you are trying to sort a single file. So to achieve any sorting at all:

<?php foreach ($page->files()->sortBy('sort') as $file): ?>
  <?php if($file->type() == 'image'): ?>
     <div><figure><img src="<?= $file->url() ?>" alt=""></figure></div>
  <?php endif ?>
  <?php if($file->type() == 'video'): ?>
    <video width="auto" height="fit-content" controls controlslist="nodownload, noremoteplayback" disablePictureInPicture
                        src="<?= $file->url() ?>">
    </video>
  <?php endif ?>
<?php endforeach ?>

There is no easy way to sort them in the way they appear in the gallery layout. And I don’t quite see the use case, given that in the layout they already appear as desired? I guess I’m missing something here.
However, this would sort your images according to their sort number if they are manually sorted.