Chang
Chang

Reputation: 77

Firestore sort by value

I'm trying to create a filter for my application It will filter by the datas' category, but I also want to sort them by its rating, higher rating will be on the top. I wrote the following code, but the orderby() function having issue.

        const snapshot = await firebase.firestore().collection('Locations').where(
            "category", "==", type).orderBy("rating").get();
        
        if (snapshot.empty){
            console.log('No matching documents');
            return;
        }
        
        snapshot.forEach(doc=> {
            locList.push(doc.data());
        })

and here is my firestore data look like firestore data

Upvotes: 0

Views: 505

Answers (1)

Adam Neuwirth
Adam Neuwirth

Reputation: 547

To sort values from largest rating to smallest rating, specify the ordering as descending. Change the orderBy function to:

.orderBy('rating', 'desc')

Upvotes: 1

Related Questions