Sorting Multiselect entries

Hey Ho,

maybe anyone can help by sorting Multiselect values alphabetical in my Template.
In the panel there is all fine caused by “sort: true”.

my_multiselect_field:
  label: Labeltitle
  type: multiselect
  search: false
  sort: true
  options:
    - 18:00
    - 18:30
    - 19:00
    - …

…yml

<?php foreach ($page->my_multiselect_field()->split('|') as $category): ?>
  <?= $category ?>
<?php endforeach ?>

template.php

I have try out:

->flip()
->sort('flip')
->sortBy('flip')
->sort()
->sortBy()
->sort('asc')
->sort('desc')
->sort('asc')
…

It seems like no command does anything.

THX in advance!

This here returns an array, so you cannot call any collection methods here and need to resort to standard php sorting of arrays.

Hey,

i don’t know exactly if this is correct. But in this way I could sort the entries from multiselect field and its the same order in the template than.

<?php

$field = $page->blueprint()->field('my_multiselect_field');
$my_multiselect_field = $page-> my_multiselect_field()->split(',')

;?>
        
<?php foreach ($my_multiselect_field as $category): ?>
  <?php echo $field['options'][$category] ?? html($category); ?>
<?php endforeach ?>

Maybe anyone else is searching for sth like that. For me it works fine :wink:
Regards

Hey there after some more hours I´ve figured it out!

Maybe anyone else is searching for a solution.

First in the blueprint set sort to true. So Users can select itmes later and they will be also in order.

my_multiselect_field:
  sort: true
  …

In the template the magic was frightening easy :wink:

sort($my_multiselect_field);

And finally:

<?php
$field = $page->blueprint()->field('my_multiselect_field'); 
$my_multiselect_field = $page->my_multiselect_field()->split(); 
sort($my_multiselect_field);
?>

<ul>
<?php foreach ($my_multiselect_field as $category): ?>
  <li><?php echo $field['options'][$category] ?? html($category); ?></li>
<?php endforeach ?>
</ul>

Regards
Martin