Open n1ckoates opened 4 days ago
This definitely makes sense, but I'm not sure what the API should look like. Can it only change the order of the array, can it change the structure of the documents, and has the output to be an array? I have to think about it.
As I think about it more, I wonder if it makes sense to build such a function into the core. It is not easy to get the API right. What if a user needs the collection sorted by date and in another spot by title? This would not be possible with such an API, but sorting can be easily achieved outside of content collections, without repeating it in any spot:
Just create a file for example sorted.ts
:
import { allPosts } from "content-collections";
export const postsSortedByDate = allPosts.toSorted(
(a, b) => b.date.getTime() - a.date.getTime()
);
And if you want to use the sorted posts, just import them e.g.:
import { postsSortedByDate } from "sorted";
// do something with the sorted posts
I think it is better to add a hint to the documentation instead of adding a new API. What do you think @n1ckoates?
I agree, that makes a lot more sense!
Just found out about Content Collections, really enjoy this library!
It'd be cool if we could run some transformation to the entire collection array before it's written to the filesystem. I'm wanting to sort my blog posts by creation date, and I don't see a way to do that without sorting them in every spot I import posts.
API could be something like this: