Forum

Thread tagged as: Question

Collections - sort by category, sort by multiple items

Is there a way to sort a collection based on a category (rather than a text field)? It doesn't seem to be working. I guess you'd need to specify which field of the category you're wanting to sort by?

Here's my template:

<perch:before>
<table>
    <thead>
        <tr>
            <th>
                Parish
            </th>
            <th>
                Borough
            </th>
            <th>
                Project
            </th>
            <th>
                Activity type
            </th>
            <th>
                Area of concern
            </th>
        </tr>
    </thead>
    <tbody>
</perch:before>
    <perch:if different="area">
    <tr>
        <th>
            <perch:categories id="area" set="areas-geographical" label="Area" required="true"><perch:category id="catTitle" /></perch:categories>
        </th>
    </tr>
    </perch:if>
    <tr>
        <td><perch:content id="parish_name" type="text" label="Parish Name" title="true" /></td>
        <td><perch:categories id="borough" set="boroughs" label="Borough" required="true"><perch:category id="catTitle" /></perch:categories></td>
        <td><perch:content id="description" type="text" label="Description/Project name" title="true"  /></td>
        <td><perch:categories id="activity_type" set="activity-type" label="Activity Type" required="true"><perch:category id="catTitle" /></perch:categories></td>
        <td><perch:categories id="area_of_concern" set="area-of-concern" label="Area of concern" required="true"><perch:category id="catTitle" /></perch:categories></td>
    </tr>
<perch:after>
    </tbody>
</table>
</perch:after>

And here's my perch_collection tag:

            <?php
                perch_collection('Projects', [
                    'template'   =>'projects_table.html',
                    'sort'       =>'parish_name',
                    'sort-order' =>'ASC',
                    'filter' => 'status',
                    'match' => 'neq',
                    'value' => 'archived'
                ]); 
            ?>

I guess the syntax if I wanted to sort by the title of the area category would be

'sort' => 'area.catTitle'?

Also, is there a way to sort on multiple items (similar to the way you can filter on multiple items).

I guess the syntax would go something like:

            <?php
                perch_collection('Projects', array(
                    'template'   =>'projects_table.html',
                    'sort'       =>array(
                        array(
                            'sort' => 'parish_name',
                            'sort-order' => 'ASC'
                        ),
                        array(
                            'sort' => 'description',
                            'sort-order' => 'ASC',
                            'sort-type' => 'numeric'

                        ),
                    ),
                    'sort-order' =>'ASC',
                    'filter' => 'status',
                    'match' => 'neq',
                    'value' => 'archived'
                )); 
            ?>

Both of those things would be helpful on this project - not sure how easy they are, but would think that being able to sort by more than one field could be useful for a number of people.

Paul Bell

Paul Bell 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

How would that work with multiple categories?

Say I have want to have all my Homelessness projects together, but then within that, have them ordered by the parish name. So I choose which category to order by first, then which to order by second. (When two items have the same first sort value, what decides which order they appear in?)

I could probably work around this by outputting my categories, and then calling perch_collection for each category,

Doesn't necessarily have to be multiple categories though, multiple fields would be useful too.

Can you even sort by category? Or are they handled separately?

Drew McLellan

Drew McLellan 2638 points
Perch Support

If I were to set a collection to sort by category, and an item was in, say, 15 categories, how would Perch know how to sort that? The next item might be in 8 completely different categories - which item should be listed first?