Add sort option to Collection glob() loader
#1315
Replies: 1 comment
-
|
+1 from me, I'm also missing this feature. Though I would like to see this as an option for My use-case is as follows. I have a file [
"./photos/edinburgh_06.jpg",
"./photos/edinburgh_09.jpg",
"./photos/paris_2019_11.jpg",
"./photos/den_haag_07.jpg",
"./photos/amsterdam_02.jpg"
]Then I define a collection based on this file with the loader: file(
'src/content/photos.json',
{
parser: (json) => {
const content = JSON.parse(json);
return content.map((image: string) => ({
id: image,
image,
}))
}
}
)The problem is that the collection is sorted alphabetically by ID (at least in the moment, looks like this isn't guaranteed). So I would have to manually sort the collection everywhere I want to use it. That's annoying and easy to forget. As a workaround, I've modified the IDs to make sure they match the order I want: id: `${String(index).padStart(4, '0')}-${image}`,But that isn't guaranteed to work, as the sort order of collections isn't guaranteed, either. This could be improved in several ways, for example:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Currently, content collections are generated unsorted(just by file name?).
Proposal: Pre-sort collections at generation time via
glob():Pages then consume pre-sorted data directly:
Benefits:
Optional, fully backward compatible.
Beta Was this translation helpful? Give feedback.
All reactions