Q:

how to order by timestamp using Firebase?

I'm trying to order by timestamp in Firebase using orderByChild() but it's not working. The results seem to be ordered by the key and Firebase seems to be ignoring orderByChild().
As you can see from the time, these results are not ordered by timestamp.

lorem-ipsum-dolor-si - 17/6/2016 a las 8:51:33
lorem-ipsum-dolor-si193 - 17/6/2016 a las 8:51:37
lorem-ipsum-dolor-si297 - 17/6/2016 a las 8:51:43
lorem-ipsum-dolor-si402 - 17/6/2016 a las 8:51:38

This is how I get the data:
let endpoint = 'texts/-KKPSjKwelbXG6Rq8AEh/-KKQqtg_a95p4Gm13XgZ'
firebase.database().ref(endpoint).orderByChild('timestamp').on('value',...)

And since snapshot.val() will be ordered by the keys, and not by the orderByChild() order, I have to store the snapshot ordered in an array and send it back to the view.
this.data = []

snapshot.forEach(function(child) {
    this.data.push(child.val())
}.bind(this))

return this.data

The timestamp comes from Date.now(). I've tried storing it as Number or String with the same result.
It's storing fine in Firebase as you can see:
{
  "-KKPSjKwelbXG6Rq8AEh" : {
    "-KKQqtg_a95p4Gm13XgZ" : {
      "lorem-ipsum-dolor-si" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171493149
      },
      "lorem-ipsum-dolor-si193" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171497193
      },
      "lorem-ipsum-dolor-si297" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171503297
      },
      "lorem-ipsum-dolor-si402" : {
        "body" : "\t<body>\n\t\tLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\t</body>",
        "timestamp" : 1466171498402
      }
    }
  }
}

What am I missing?
Edit:
The problem was that I was creating the reference and later adding orderByChild().
let ref = firebase.database().ref(endpoint)
...
ref.orderByChild('timestamp')
...
ref.on('value',onChange)

I was doing that because I have a class that creates these calls to Firebase dynamically.

A:

Works for me:
firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("all", data.map(function(val) { return new Date(val.timestamp).toString(); }));
});

"all"
["Fri Jun 17 2016 06:51:33 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:43 GMT-0700 (PDT)"]
  Blockquote

firebase.database().ref(endpoint)
  .orderByChild('timestamp')
  .startAt(1466171497190)
  .endAt(1466171498405)
  .on('value', function(snapshot) {
  this.data = [];

  snapshot.forEach(function(child) {
    this.data.push(child.val());
  }.bind(this));

  console.log("filtered", data.map(function(val) { return new Date(val.timestamp).toString(); }));

});

"filtered"
["Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)"]

http://jsbin.com/humatuquju/edit?js,console

