1

This is the code that I have: I am trying to get data stored in the Firebase Database sorted by wage. The order of the database goes: "Posts -> PostID(.push when saving) -> Wage." I have the data retrieval working just not in order.

var PostsRef = firebase.database().ref().child("Posts");
PostsRef.on('child_added', function(data) {
  var title = data.val().title;
  var description = data.val().description;
  var completionDate = data.val().completionDate;
  var complexity = data.val().complexity;
  var wage = data.val().wage;
  var uid = data.val().userID;
});
Icarus
  • 1,627
  • 7
  • 18
  • 32

1 Answers1

0

You're storing the wages as string, which means that they will be sorted in lexicographical order:

1

13

2

20

3

If you want to get the results in numerical order, you should store the wages as numbers, so without the quotes.

If you store them from your code, this might require that you convert it with parseInt(wagesAsString).

After you store the wages as numbers, you can get them in order with:

var PostsRef = firebase.database().ref().child("Posts");
var query = PostsRef.orderByChild("wage");
query.on('child_added', function(data) {
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807