title: how to order by timestamp using Firebase?
body: <p>I'm trying to order by timestamp in Firebase using <code>orderByChild()</code> but it's not working. The results seem to be ordered by the key and Firebase seems to be ignoring <code>orderByChild()</code>.</p>
  
  <p>As you can see from the time, these results are not ordered by timestamp.</p>
  
  <blockquote>
    <p>lorem-ipsum-dolor-si - 17/6/2016 a las 8:51:33</p>
    
    <p>lorem-ipsum-dolor-si193 - 17/6/2016 a las 8:51:37</p>
    
    <p>lorem-ipsum-dolor-si297 - 17/6/2016 a las 8:51:43</p>
    
    <p>lorem-ipsum-dolor-si402 - 17/6/2016 a las 8:51:38</p>
  </blockquote>
  
  <p>This is how I get the data:</p>
  
  <pre><code>let endpoint = 'texts/-KKPSjKwelbXG6Rq8AEh/-KKQqtg_a95p4Gm13XgZ'
  firebase.database().ref(endpoint).orderByChild('timestamp').on('value',...)
  </code></pre>
  
  <p>And since <code>snapshot.val()</code> will be ordered by the keys, and not by the <code>orderByChild()</code> order, I have to store the snapshot ordered in an array and send it back to the view.</p>
  
  <pre><code>this.data = []
  
  snapshot.forEach(function(child) {
      this.data.push(child.val())
  }.bind(this))
  
  return this.data
  </code></pre>
  
  <p>The timestamp comes from <code>Date.now()</code>. I've tried storing it as <code>Number</code> or <code>String</code> with the same result.</p>
  
  <p>It's storing fine in Firebase as you can see:</p>
  
  <pre><code>{
    "-KKPSjKwelbXG6Rq8AEh" : {
      "-KKQqtg_a95p4Gm13XgZ" : {
        "lorem-ipsum-dolor-si" : {
          "body" : "\t&lt;body&gt;\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&lt;/body&gt;",
          "timestamp" : 1466171493149
        },
        "lorem-ipsum-dolor-si193" : {
          "body" : "\t&lt;body&gt;\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&lt;/body&gt;",
          "timestamp" : 1466171497193
        },
        "lorem-ipsum-dolor-si297" : {
          "body" : "\t&lt;body&gt;\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&lt;/body&gt;",
          "timestamp" : 1466171503297
        },
        "lorem-ipsum-dolor-si402" : {
          "body" : "\t&lt;body&gt;\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&lt;/body&gt;",
          "timestamp" : 1466171498402
        }
      }
    }
  }
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>Edit:</p>
  
  <p>The problem was that I was creating the reference and later adding <code>orderByChild()</code>.</p>
  
  <pre><code>let ref = firebase.database().ref(endpoint)
  ...
  ref.orderByChild('timestamp')
  ...
  ref.on('value',onChange)
  </code></pre>
  
  <p>I was doing that because I have a class that creates these calls to Firebase dynamically.</p>