Sort/Order flex-objects by one of the objects’ properties?

I am really struggling to make the plugin flex-objects work for a discography directory I need for a musician’s website.

Nevertheless, I am at the point where I can display a simple list from that directory in the frontend. But I cannot find a way to sort that list by one of the properties (release_year). No matter what I tried, the list will always be displayed in the order in which the entries have been stored in the YAML data file.

I have unsuccessfully tried to add options.order in several places in my /user/blueprints/flex-objects/discography.yaml because it does work for the backend listing:

config:
  admin:
    list:
      options:
        by: release_year
        dir: desc

I have also tried some Twig functions I had used with the old flex-directory plugin – but to no effect:
{% set collection = directory.collection.toArray()|sort_by_key(‘release_year’) %}
doesn’t do anything; just like
{% for object in collection.toArray()|sort_by_key(‘release_year’) if object.published %}

I can also drop .toArray() and nothing will change.

Then I found the Grav\Framework\Flex\FlexCollection->sort() and tried
{% set collection = directory.collection.sort({‘release_year’: 'DESC'}) %}
Which also did not do anything.

I’m completely out of my depths and very grateful about any help.

2 Likes

I found the answer. I had somehow used typographic single quotes (‘’) instead straight ones ('') for the property name. Alternatively it works without quotes as well.

(1) {% set collection = directory.collection.sort({'release_year': 'DESC'}) %}
(2) {% set collection = directory.collection.sort({release_year: 'DESC'}) %}

2 Likes