title: Order UITableView cells by calculated distance (Parse)
body: <p>I have a problem with the database created on parse. I have a table that shows me how many miles of the various local (once you download the list from DB) the problem is that I would order it for distance upward, but I do not know how to do. The code I used to calculate the distance is this:</p>
  
  <pre><code>- (id)initWithCoder:(NSCoder *)aCoder
  {
      self = [super initWithCoder:aCoder];
      if (self) {
          // Custom the table
  
          // The className to query on
          self.parseClassName = @"Ristoranti";
  
          // The key of the PFObject to display in the label of the default cell style
          self.textKey = @"NomeLocale";
  
          // Whether the built-in pull-to-refresh is enabled
          self.pullToRefreshEnabled = YES;
  
          // Whether the built-in pagination is enabled
          self.paginationEnabled = YES;
  
          // The number of objects to show per page
          self.objectsPerPage = 100;
      }
      return self;
  }
  
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      locationManager.distanceFilter = kCLDistanceFilterNone;
      locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
  
      if(IS_OS_8_OR_LATER) {
  
          [locationManager requestWhenInUseAuthorization];
          [locationManager requestAlwaysAuthorization];
      }
  
      [locationManager startUpdatingLocation];
  
      [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(refreshTable:)
                                                   name:@"refreshTable"
                                                 object:nil];
  }
  
  - (void)refreshTable:(NSNotification *) notification
  {
      // Reload the recipes
      [self loadObjects];
  }
  
  
  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshTable" object:nil];
  }
  
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  }
  
  - (PFQuery *)queryForTable
  {
      PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
  
      query.limit = 100;
  
      [query orderByAscending:@"createdAt"];
  
      return query;   
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
  {
      static NSString *simpleTableIdentifier = @"RecipeCell";
  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
      if (cell == nil) {
  
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
      }
  
      NSString *AntDef = [NSString stringWithFormat:@"%@", [object objectForKey:@"AnteprimaDefault"]];
  
      if ([AntDef isEqualToString:@"Null"])
      {   
          PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
          thumbnailImageView.image = [UIImage imageNamed:@"ImageDefault.png"];
          thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.width / 2;
          thumbnailImageView.clipsToBounds = YES;
          thumbnailImageView.layer.borderWidth = 0.5;
          thumbnailImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
      }
  
      else
      {   
          PFFile *thumbnail = [object objectForKey:@"Anteprima1"];
          PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
          thumbnailImageView.image = [UIImage imageNamed:@"placeholder.jpg"];
          thumbnailImageView.file = thumbnail;
          thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.width / 2;
          thumbnailImageView.clipsToBounds = YES;
          [thumbnailImageView loadInBackground];
      }
  
  
  
      Lat = [[object objectForKey:@"Latitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
      Long = [[object objectForKey:@"Longitudine"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  
      Latdouble = [Lat doubleValue];
      Longdouble = [Long doubleValue];
  
  
      NSArray *locations = [NSArray arrayWithObjects:[[CLLocation alloc] initWithLatitude:Latdouble longitude:Longdouble], nil];
  
      //NSLog(@"LOCATIONS COORDINATE: %@", locations);
  
      CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];;
      CLLocation *nearestLoc = nil;
      CLLocationDistance nearestDis = FLT_MAX;
  
      for (CLLocation *location in locations) {
          CLLocationDistance distance = [currentLocation distanceFromLocation:location];
  
          for (CLLocation *location in locations) {
              distance = [currentLocation distanceFromLocation:location];
              if (nearestDis &gt; distance) {
                  nearestLoc = location;
                  nearestDis = distance;
              }
          }
  
          text12 = [NSString  stringWithFormat:@"%.1f Km", nearestDis/1000];
      }
  
  ...
  }
  </code></pre>
  
  <p>Thanks in advance :)</p>