1

When I create new note, it orders old to new, but I want to order new to old (reverse it). How can i do this ?

my codes:

const notesRef = useFirestore().collection('users').doc(uid).collection("notes");
  const {status, data} = useFirestoreCollection(notesRef.orderBy("timezone"));

and its image: (Here, it order like 1-2-3, but i want to order, 3-2-1, new to old) enter image description here

our return like map:

 {data?.docs?.map((d, index) => {
              return (<Note
                      key={index}
                      id={index}
                      title={d.data().title}
                      content={d.data().content}
                      onDelete={deleteNote}
                      onDocId={d.id}
                      timezone={d.data().timezone}
                    />);
            })}

2 Answers 2

1

To sort a query in descending order, you can pass a second parameter to orderBy. So:

notesRef.orderBy("timezone", "desc")

Also see the Firebase documentation on ordering data.

Sign up to request clarification or add additional context in comments.

Comments

1

There are a bunch of ways of doing this.

  • Fetch the data in reversed order from firebase. I am using 25 as an example. You can choose the limit as per your requirements.
notesRef.orderBy("timezone").limitToLast(25);
  • Reverse the data on the client-side
const {status, data} = useFirestoreCollection(notesRef.orderBy("timezone"));
console.log(data.docs.reverse())

5 Comments

what is the limitToLast value?
i tried to limitToLast(10), but it didn't work. Also I have a map (I added above), and i add data?.reverse() function, but it also didn't work
you can look at the docs here: firebase.google.com/docs/reference/js/…
i have updated my answer after looking at the data. you can reverse the data.docs array.
thanks a lot. Also look other answer, if i write "desc" near the timezone, it also work. But doc.reverse() also work. <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.