user8737417
user8737417

Reputation:

Ordering Data In FireStore

I have numeric data each in every document... I am getting the data using this code

 db.collection('Stations').orderBy("station","asc").onSnapshot((querySnapshot) =>{
  this.setState({
    stations:querySnapshot.docs.map(doc => {
      return {
        id: doc.id,
        station:doc.data().station
      }
    })
  })
}); 

And the data is not getting arranged properly as seen below:

not getting arranged properly as seen below:-

Upvotes: 2

Views: 1260

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

You're ordering by string values, which use lexicographical ordering. And in lexicographical order "100" comes before "2".

To fix your problem, have a field with only the numerical value, and order by that field. Alternatively pad the numbers so they all have the same length, because "002" comes before "100".

Also see:

Upvotes: 3

Related Questions