Skip to content

Commit

Permalink
docs(core): #169 add documentation about sorting collections
Browse files Browse the repository at this point in the history
Closes: #169
  • Loading branch information
sdorra committed Jul 13, 2024
1 parent 7c9d682 commit ee60216
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"configuration",
"transform",
"serialization",
"sorting",
"migration",
"---Quick Start---",
"...quickstart",
Expand Down
31 changes: 31 additions & 0 deletions docs/sorting.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Sorting
---

Content Collections does not offer a built-in way to sort collections. This is because it is challenging to define a universal API for sorting that fits all use cases. Implementing a single sorting method would limit the flexibility to sort collections differently in various parts of the application.

However, you can easily sort the collections in your own code. For example, you can create a file called `sorted.ts` with the following content:

```ts
import { allPosts } from "content-collections";

export const postsSortedByDate = allPosts.toSorted(
(a, b) => b.date.getTime() - a.date.getTime()
);
```

Then, you can import the sorted collection from this file instead of directly from `content-collections`.

```ts
import { postsSortedByDate } from "./sorted";

// Do something with the sorted posts
```

This approach allows you to sort your collections in any way you prefer.

<Callout>

For more information, refer to [Issue #169](https://github.com/sdorra/content-collections/issues/169).

</Callout>

0 comments on commit ee60216

Please sign in to comment.