Reputation: 127
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)
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}
/>);
})}
Upvotes: 1
Views: 781
Reputation: 305
There are a bunch of ways of doing this.
notesRef.orderBy("timezone").limitToLast(25);
const {status, data} = useFirestoreCollection(notesRef.orderBy("timezone"));
console.log(data.docs.reverse())
Upvotes: 1
Reputation: 600126
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.
Upvotes: 1