title: How can firebase users be displayed/ranked according to their distance away from each other with CLLocation?
body: <p>The page should list all users in the firebase database who are within 3 miles. Right now it just lists all the users. In addition to limiting the listing of users to those within 3 miles, it would be good to rank the displayed users, from closest to furthest.</p>
  
  <p>Below is code that already works to display all the users (within 3 miles and further) from the firebase database. All users have a location in firebase - latitude and longitude. </p>
  
  <pre><code>            for people in snapshot.children.allObjects as! [DataSnapshot] {
                  if people.key != thisUsersUid { //do not add this users info to the array
                      let peopleObject = people.value as? [String: AnyObject]
                      let peopleEducation = peopleObject?["Education"] as? String
                      let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"] as? String
                      let peoplePhotoPosts = peopleObject?["PhotoPosts"]  as? String
                      let peopleimageDownloadURL = peopleObject?["imageDownloadURL"]  as? String
                      let peoplepostID = peopleObject?["postID"] as? String
                      let peoplepeopleWhoLike = peopleObject?["peopleWhoLike"] as? String
                      let peopl = Userx(Education: peopleEducation, WhatIamConsideringBuying: peopleWhatIamConsideringBuying, PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, peopleWhoLike: peoplepeopleWhoLike)
  
                      self.people.append(peopl)
  
                  }
                  self.table.reloadData()
              }
          }
  
          })
  
          public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
  
      let immy = cell.viewWithTag(1) as! UIImageView
  
      let person: Userx = people[indexPath.row]
  
  
  
      cell.lblName.text = person.Education
  
      cell.postID = self.people[indexPath.row].postID
  
  
  
  
  
  
      if let PhotoPosts = person.PhotoPosts {
          let url = URL(string: PhotoPosts)
          immy.sd_setImage(with: url)
      }
  
      return cell
  
  }
  </code></pre>
  
  <p>/Below makes the users locations:</p>
  
  <pre><code>  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      let databaseRef = Database.database().reference()
      let uid = Auth.auth().currentUser!.uid
  
      guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
      print("locations = \(locValue.latitude) \(locValue.longitude)")
      latestLocation = ["latitude" : locValue.latitude, "longitude" : locValue.longitude]
  
      if let locationDictionary = latestLocation {
          databaseRef.child("people").child(uid).child("Coordinates").setValue(locationDictionary)
      }
  </code></pre>
  
  <p>//Update after answer: </p>
  
  <pre><code>for people in snapshot.children.allObjects as! [DataSnapshot] {
  
  .......
  
  let peoplepeopleWhoLike = peopleObject?["peopleWhoLike"] as? String
  let userId = people.key
  let coordSnap = people.childSnapshot(forPath: "Coordinates")
  let lat = coordSnap.childSnapshot(forPath: "latitude").value as! CLLocationDegrees
  let lon = coordSnap.childSnapshot(forPath: "longitude").value as! CLLocationDegrees
  let locCoord = CLLocationCoordinate2DMake(lat, lon)
  let coordinates  = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  let peopleLocation = (lat, lon)
  print(userId, "coords: \(lat)  \(lon)")
  let distance = peopleLocation.distance(to: latestLocation)
  let peopl = Userx(Education: peopleEducation, WhatIamConsideringBuying: peopleWhatIamConsideringBuying, PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, peopleWhoLike: peoplepeopleWhoLike, distance: distance)
  </code></pre>
  
  <p>Right Now: All users randomly displayed below each other.
  What I need: Users within 3 miles displayed underneath each other, from closest to furthest.</p>