Sort FileList by Custom Attribute

Permalink
I have added a custom date attribute to files in a File Set. I need to sort the file list by this date, but I can't figure out how to sort by this date.

Are custom attributes accessible to the File Set object?

 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi jmartinwestern,

"Searching and Sorting with the FileList Object"
https://www.concrete5.org/documentation/developers/5.7/working-with-...
jmartinwestern replied on at Permalink Reply
Thanks, but I don't think that is what I'm looking for. Won't that only return files that are from a specific date? I need to order the output of the files by date.
MrKDilkington replied on at Permalink Reply
MrKDilkington
You can try this:
$list = new \Concrete\Core\File\FileList();
// the file set name is "Test Set"
$set = \Concrete\Core\File\Set\Set::getByName('Test Set');
$list->filterBySet($set);
// the file attribute handle is "file_date"
// prefix the file attribute handle with "ak_"
// date descending
// $list->sortBy('ak_file_date', 'desc');
// date ascending
$list->sortBy('ak_file_date', 'asc');
$files = $list->getResults();
foreach ($files as $file) {
    echo $file->getFileName() . ': ' . $file->getAttribute('file_date') . '<br>';
}