How do I sort items inside a structure field?

HI. I have a Blueprint with a structure field off members which I would like to echo on the front page and sort the yaml items by the name field inside the structure field.

The Blueprint looks like this:

members:
    label: Medlemmar
    type: structure
    entry: >
      <strong>Namn:</strong> {{name}}<br />
      <strong>E-mail:</strong> {{email}}<br />
      <strong>Ort:</strong> {{city}}<br />
      <strong>MC:</strong> {{mc}}<br />
      <strong>VRCC nr:</strong> {{vrccnumber}}
    fields:
      name:
        label: Namn
        type: text
      email:
        label: E-mail
        type: email
      city:
        label: Ort
        type: text
      mc:
        label: MC
        type: text
      vrccnumber:
        label: VRCC medl.nr
        type: text

And in the template I would like to do some time like this:

<?php foreach($page->members()->yaml()->sortBy( $page->members()->yaml()->name(), 'desc' ) as $member): ?>
        <section class="member cf negative-text">
          <div class="member-name"><?php echo $member['name'] ?><br><span class="member-email"><a href="mailto:<?php echo $member['email'] ?>"><?php echo $member['email'] ?></a></span></div>
          <div class="member-city"><?php echo $member['city'] ?></div>
          <div class="member-mc"><?php echo $member['mc'] ?></div>
          <div class="member-vrccnr"><?php echo $member['vrccnumber'] ?></div>
        </section>
      <?php endforeach ?>

How do I do it correctly? I read this post: Can i use filterBy on a structure field? and tried:

<?php
$members = $page->members()->toStructure();
foreach($page->members()->yaml()->sortBy( $members->name(), $direction = 'desc' ) as $member):
?>

But it did’n work for me.

This should work:

<?php $members = $page->members()->toStructure()->sortBy('name', 'asc') ?>

<ul>
  <?php foreach($members as $member): ?>
    <li><?php echo $member->name() ?></li>
  <?php endforeach ?>
</ul>
2 Likes

Works perfect, thank you!