简体   繁体   中英

how can i sort firebase timestamp data reverse reactjs

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}
                    />);
            })}

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())

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM