title: Ordering of data with Firebase Android
body: <p>I'm running into a weird issues in which a Firebase query using <code>orderByChild()</code> doesn't actually order the data. Below is a snapshot of the data that I'm trying to order: (total is off for the sake of this example)</p>
  
  <p><a href="https://i.stack.imgur.com/NOkGV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NOkGV.png" alt="Snapshot of data"></a></p>
  
  <p>This is the code that I've used so far:  </p>
  
  <pre><code>Query query = locationComment.orderByKey();
      query.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              if (dataSnapshot.getValue() == null) {
                  return;
              }
              data.clear();
              adapter.notifyDataSetChanged();
              String userId;
              String time;
              String comment;
              Map&lt;String, String&gt; commentMap = (Map) dataSnapshot.getValue();
              for (Map.Entry&lt;String, String&gt; entry : commentMap.entrySet()) {
                  if(!((entry.getKey()).contains("total"))) {
                      String[] keyString = (entry.getKey()).split(",");
                      time = keyString[0];
                      userId = keyString[1];
                      comment = entry.getValue();
                      Date resultdate = new Date(Integer.parseInt(time));
                      data.add(new Comment(comment,
                              DateFormat.getDateTimeInstance().format(resultdate), userId));
                      adapter.notifyItemInserted(data.size());
                  }
              }
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  </code></pre>
  
  <p>I'm getting each and every key-value pair (barring total) except that it's not in order as expected (ordered by key). The Firebase docs say that the keys are ordered lexicographically if they can't be parsed into a 32 bit integer.</p>
  
  <p>Either way though the order should be as shown in the image but the data that I get back while looping through the map is not in this order. </p>
  
  <p>I would really appreciate it if someone could point me in the right direction. Thanks!</p>