title: Firebase getting data in order
body: <p>I am using Firebase and I have had no problems getting data in alphabetical order until recently. I never used queries, I always just used snapshots of data and sorted through them one-by-one. Recently, the data has not been always coming in alphabetical order in the <strong>snapVal</strong>. How do I make it so I get a snapVal of data sorted alphabetically, like it is in the snapshot from the database?</p>
  
  <p>Real Example: there are 4 messages, id1-id4 (in that order). They contiain the message "1"-"4". The snapshot comes looking correct. But the snapVal (snapshot.value) looks like this:</p>
  
  <pre><code>["id2": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 2;
      TIME = "8:12 PM";
  }, "id4": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 4;
      TIME = "8:12 PM";
  }, "id1": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 1;
      TIME = "8:12 PM";
  }, "id3": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 3;
      TIME = "8:12 PM";
  }]
  </code></pre>
  
  <p>What the snapshot looks like:</p>
  
  <pre><code>["id1": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 1;
      TIME = "8:12 PM";
  }, "id2": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 2;
      TIME = "8:12 PM";
  }, "id3": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 3;
      TIME = "8:12 PM";
  }, "id4": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 4;
      TIME = "8:12 PM";
  }]
  </code></pre>
  
  <p>To get the snapVal, I use this:</p>
  
  <pre><code>if let snapVal = snapshot.value as? [String: AnyObject] {
       // Comes out of order..
  }
  </code></pre>
  
  <p>To clarify:</p>
  
  <p>Snapshot (this ends up coming out correct):</p>
  
  <pre><code>Snap (CHAT) {
      id1 =     {
          DATE = "10/20/16";
          "FIRST_NAME" = first;
          ID = userID;
          "LAST_NAME" = last;
          MESSAGE = 1;
          TIME = "8:12 PM";
      };
      id2 =     {
          DATE = "10/20/16";
          "FIRST_NAME" = first;
          ID = userID;
          "LAST_NAME" = last;
          MESSAGE = 2;
          TIME = "8:12 PM";
      };
      id3 =     {
          DATE = "10/20/16";
          "FIRST_NAME" = first;
          ID = userID;
          "LAST_NAME" = last;
          MESSAGE = 3;
          TIME = "8:12 PM";
      };
      id4 =     {
          DATE = "10/20/16";
          "FIRST_NAME" = first;
          ID = userID;
          "LAST_NAME" = last;
          MESSAGE = 4;
          TIME = "8:12 PM";
      };
  }
  </code></pre>
  
  <p>This is the output for <code>print(snapVal.keys)</code> inside <code>if let snapVal = snapshot.value as? [String: AnyObject]</code>:</p>
  
  <pre><code>LazyMapCollection&lt;Dictionary&lt;String, AnyObject&gt;, String&gt;(_base: ["id2": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 2;
      TIME = "8:12 PM";
  }, "id4": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 4;
      TIME = "8:12 PM";
  }, "id1": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 1;
      TIME = "8:12 PM";
  }, "id3": {
      DATE = "10/20/16";
      "FIRST_NAME" = first;
      ID = userID;
      "LAST_NAME" = last;
      MESSAGE = 3;
      TIME = "8:12 PM";
  }], _transform: (Function))
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>  self.firebase.child("Chats").child(chatID).queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in
                            print(snapshot)
                          if let snapVal = snapshot.value as? [String: AnyObject] {
  
                              print(snapVal)
  
                              for c in snapVal {
                                  print("checking Message as child")
                                  let message = c.value["MESSAGE"] as? String
  
                                  let fn = c.value["FIRST_NAME"] as? String
                                  let ln = c.value["LAST_NAME"] as? String
  
                                  let USER_ID = c.value["ID"] as? String
  
                                  if let userID = USER_ID {
                                      if let msg = message {
                                          if let firstName = fn {
                                              if let lastName = ln {
                                                  let username = "\(firstName) \(lastName)"
                                                  self.addMessage(userID, text: msg, name: username)
  
                                                  print("Message added! \nMessage Info:")
                                                  print("User ID: \(userID)")
                                                  print("text: \(msg)")
                                                  print("Username: \(username)")
                                              } else {
                                                  print("LN did not pass")
                                              }
                                          } else {
                                              print("FN did not pass")
                                          }
                                      } else {
                                          print("Msg did not pass")
                                      }
                                  } else {
                                      print("User ID did not pass")
                                  }
                              }
                            }  
  })
  </code></pre>