Changing Plone's folder listing sort order
Purpose
To change the order in which folder_listing and folder_contents list items. There isn't an obvious way to change this, and sorting by hand isn't an option when you have folders containing many items, and more constantly coming in.
Method 1: Use a Collection (recommended)
The easy, all-Plone way to do this is to create a Collection (formerly known as Smart Folder, or Topic) which you can configure through the web to display items from wherever you please, in whatever order you please. You should consider using a Collection first. For example, look how the "News" folder is set up in in a fresh Plone install. It contains a Collection which is used as the default view for the folder.
See the documentation on using Collections.
Method 2: Customize the getFolderContents script (not recommended)
Prerequisites
Some Plone scripting knowledge is required.
Step by step
The folder_listing and folder_contents views use the script getFolderContents (in Products.CMFPlone, inside the plone_scripts skin layer) to get brain objects.
If
you want to modify the default order in which items are listed in your
site, you can do so by modifying the contentFilter dictionary. As you can see in the getFolderContents script, the contentFilter dictionary is used in the Catalog Tool query.
For example, to sort elements on modified date in descending order, add the lines in bold:
# Modification to alter sort order
contentFilter['sort_on'] = "modified"
contentFilter['sort_order'] = "descending"
if not contentFilter.get('sort_on', None):
...
See Indexing and Searching - Querying the Catalog for more information about sorting.
Sorting different folders in a different way
If you wanted a different sort order for different folders, you could place the customized script in the folder itself. Acquisition should take care that the script in the folder is found before the script in the site root.
Caveats
This will disable the feature that allows you to sort the folder by hand in a nasty way: the view of the "Contents" tab in a folder will still allow you to move items up or down in the ordering. It does this because it doesn't get a sort_on key in its contentFilter parameter. But manual ordering won't work. So this may be confusing.
This method also changes the sort order for anything that uses the getFolderContents script. If you want to modify the behavior for a specific template, see the next method.
Method 3: Modifying a specific listing template
Prerequisites
Basic TAL templating knowledge and basic understanding of Python dictionaries. See the Zope Page Templates tutorial for an introduction to TAL.
Step by step
Find the template that you want to modify (for our example we will modify the folder_listing.pt). This template contains a tal definition for the contentFilter. Here is what it looks like by default:
contentFilter contentFilter | request/contentFilter| nothing;
This can be changed to pass a dictionary with the sort options you desire. If we want to make the same modifications as the getFolderContents above, we would use the following define:
custom_sort python:{'sort_on': 'modified', 'sort_order': 'descending'};
contentFilter contentFilter | request/contentFilter| custom_sort;
Now the folder_listing template will be sorting on the modified index. You could even create your own copy of this template, change the name of it, then add it as an available view for folders; this would give you the ability to apply the custom sorting to selected folders in the site using the Display dropdown.
Caveats
The biggest caveat here is that you will need to maintain the edits to the template for each Plone upgrade.
