Sort values within multiselect fields in Object field

Hi all,
I am using the object field for the first time. It hast 5 fields and each of those is a multiselect field to select different values. After the page gets updated with new values within these 5 fields of the object field, I’d like to order the values of each field alphabetically.

I’ve done this in the past with values within single multiselect fields but I don’t find the right way to select each field within the object field and get + split + sort the values.

Any help is greatly appreciated!

For display in the Panel (i.e. in a hook) or when rendering the field?

Oh, sorry for this missing detail! I want to do this with a hook for display in the Panel.

You can probably make your life easier with the sort: true option applied to the multiselect fields and ordering your options in the desired order instead of updating the field in a hook?

Of course, that’s all I need! The options are already ordered.

Now I see that the sort:true option of the multiselect field orders the entries according to the alphabetical order from the dropdown, but this is only reflected in the object field fly-over menu and not in the main panel interface when the field is not in editing mode. Also the order in the content file is different than the order in the fly-over menu.

Ok, then back to the hook, so inside your hook, you could do something like this:

$object = $newPage->object()->toObject()->toArray();

array_walk( $object, function (&$item) {
   $items = explode(',', $item);
   sort($items, SORT_NATURAL);
   $item =  implode(',', $items);
 });

$newPage = $newPage->update([
  'object' => $object,
]);

Thank you so, so much!