Search code examples
sortingmeteormeteor-collection2

Is it possible to sort a collection only once and then keep that order intact despite reactivity?


I have a list of Comments. These comments have an attribute called "vote" (users can vote comments) and they are initially sorted by votes (descending, onRender).

Right now, when users vote, the order of the comments is reactively updated to reflect the new votes.

Is it possible to somehow keep the initial sorting order intact? I would like to avoid confusing the user with comments automatically swapping order while she/he is on the page.

Is there any good way to solve this? I was thinking perhaps of a one-time sort when rendering the page, or somehow saving the order and then reapplying it whenever the collection is reactively refreshed.


Solution

  • You could use a function to sort your Minimongo query. So something like:

    const initialVotes = new Map();
    Comments.find({}, {sort: (a, b) => {
      if (!initialVotes.has(a._id)) initialVotes.set(a._id, a.votes);
      if (!initialVotes.has(b._id)) initialVotes.set(b._id, b.votes);
    
      return initialVotes.get(b._id) - initialVotes.get(a._id);
    });
    

    This will make it so that comments are sorted by initial votes. If anything else changes (like user edits the comments), that will reactively propagate, if a new comment is made, it will reactively be added. But if votes change, order will not change (but the vote number maybe rendered will still update).