'How to order pieces
I have a problem showing the contents of the pieces.
I would like the pieces to be shown in "apostrophe-pieces-pages" by their date of creation. I've tried mongoDB queries to sort them by the "createdAt" field through the "BeforeSend" module, however this result appears on all pages and I don't want that result.
Would there be any way to sort by creation date that only affects the page of the piece?
Solution 1:[1]
apostrophe-pieces has a top level option for adding a simple sort filter to your index. In your pieces subclass (your module that extends apostrophe-pieces, not apostrophe-pieces-pages) you should be able to add a sort filter to your module options for ordering.
module.exports = {
  label: 'My Piece',
  extend: 'apostrophe-pieces',
  sort: { createdAt: -1 } // reverse chronological order
  // ... other configuration
};
More on sort at https://docs.apostrophecms.org/apostrophe/modules/apostrophe-docs/server-apostrophe-cursor#sort-value
More on the MongoDB sort method and what it takes here https://docs.mongodb.com/manual/reference/method/cursor.sort/
If you wanted to create completely custom orderings, the apostrophe-pieces-orderings-bundle might be helpful https://github.com/apostrophecms/apostrophe-pieces-orderings-bundle
Solution 2:[2]
In apostrophe 3 the ordering can be set by:
module.exports = {
  extend: '@apostrophecms/piece-type',
  options: {
    sort: {
      publishDate: -1
    }
  },
  ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | |
| Solution 2 | Toxus | 
