Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7898649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:29:25+00:00 2026-06-03T08:29:25+00:00

I have a custom UITableView populated by JSON data brought into a NSMutableDictionary. I

  • 0

I have a custom UITableView populated by JSON data brought into a NSMutableDictionary. I then create a mutablecopy so that I can add the value of the distanceFromLocation method to the dictionary. What I am trying to do is to then sort the the cells by the closest distance using that new object. I have looked at other examples of using NSSortDescriptor, but that is talking about sorting arrays. How can I either sort the cells from the dictionary or create an array from the dictionary to use the NSSortDescriptor?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

static NSString *CellIdentifier = @"dealerlistcell";

DealerListCell *cell = (DealerListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[DealerListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

//gets JSON data and loads into Dictionary
NSMutableDictionary *info = [json objectAtIndex:indexPath.row];
NSMutableDictionary *infocopy = [info mutableCopy];

//pulls the current user location
CLLocation *currentlocation2 = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];

//gets the latitude and longitude from info dictionary to calculate distancefromlocation for each cell
CLLocation *locationforindex = [[CLLocation alloc] initWithLatitude:[[info objectForKey:@"dlrlat"]doubleValue] longitude:[[info objectForKey:@"dlrlong"]doubleValue]];

//calculates the distance
CLLocationDistance dist = ([locationforindex distanceFromLocation:currentlocation2]*0.0006213711922);

//format the distance to a string for the label
NSString *distancestring = [NSString stringWithFormat:@"%.1f miles",dist];

//create object with distance to add to dictionary
NSNumber *distance = [NSNumber numberWithDouble:dist];

EDIT: Assigned distance to object to call later in NSSortDescriptor

//add to dictionary 
[infocopy setObject:distance forKey:@"distance"];

//create array from dictionary
NSDictionary *aDict = [NSDictionary dictionaryWithDictionary:infocopy];
NSArray *anArray = [aDict allValues];

//implemented NSSortDescriptor
NSSortDescriptor *sorter [[NSSortDescriptor alloc] initWithKey:@"distance" ascending: YES];
NSArray *sortdescriptors = [NSArray arrayWithObject:sorter];
[anArray sortedArrayUsingDescriptors:sortdescriptors];

However it throws valueForUndefinedKey for the key distance. How do I define the distance key in “initWithKey?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:29:27+00:00Added an answer on June 3, 2026 at 8:29 am

    Through some help from this forum and my friends, I was able to piece this solution together.

    The answer was to use a for loop and create dictionaries for each location in a method before it loads the table view. Then I used a NSSortDescriptor to do the sorting, and it worked perfectly.

    See the code below for the final solution.

    -(void)getData:(NSData *) data 
    {
    
    NSError *error;
    
    // load the jsonArray with the JSON Data
    jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    
    
    //define the unsorted dealers array
    unsortedDealers = [[NSMutableArray alloc] initWithCapacity:1000];
    
    // start at row 0
    int index = 0;
    
    // loop through each dealer and calculate distance from current location
    for (NSDictionary *dealer in jsonArray) 
    {
        //create dictionary from JSON Array
        infoDict = [jsonArray objectAtIndex:index];
    
        //get the coordidates from current location
        CLLocation *currentlocation = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
    
        //get the coordinates for dealer at row
        // dlr lat and longs are switched in the database
        CLLocation *locationforindex = [[CLLocation alloc] initWithLatitude:[[infoDict objectForKey:@"dlrlong"]doubleValue] longitude:[[infoDict objectForKey:@"dlrlat"]doubleValue]];
    
        //calculate the distance from current location to dealer at row and format it in miles
        CLLocationDistance dist = ([locationforindex distanceFromLocation:currentlocation]*0.0006213711922);
    
        //define the distance for display
        NSNumber *distanceForDict = [NSNumber numberWithDouble:dist];
    
        //define the distance for sorting
        NSString *distanceForSort = [NSString stringWithFormat:@"%.1f",dist];
    
       //create a dictionary for each dealer and define the values for each key 
       NSMutableDictionary *dealerDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[infoDict objectForKey:@"dlrname"] , @"dlrname", [infoDict objectForKey:@"dlrcity"], @"dlrcity", [infoDict objectForKey:@"dlrstate"] , @"dlrstate", [infoDict objectForKey:@"dlrzip"], @"dlrzip", [infoDict objectForKey:@"dlradd"] , @"dlradd", distanceForDict , @"dlrdistance" , distanceForSort , @"dlrsortdistance" , [infoDict objectForKey:@"dlremail"] , @"dlremail" , [infoDict objectForKey:@"dlrfax"] , @"dlrfax" , [infoDict objectForKey:@"dlrhours"] , @"dlrhours" , [infoDict objectForKey:@"dlrlat"], @"dlrlat",[infoDict objectForKey:@"dlrlong"] , @"dlrlong" , [infoDict objectForKey:@"dlrnum"], @"dlrnum",[infoDict objectForKey:@"dlrphone"] , @"dlrphone" , [infoDict objectForKey:@"dlrwebsite"], @"dlrwebsite", nil];
    
        //add dictionary to the unsorted array
        [unsortedDealers addObject:dealerDict];
    
        // iterate through the list of dealers
        ++index;
    
    }
    
    // creates the sorting element
    NSSortDescriptor *sortDescriptor;
    
    //define what you want to sort by
    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dlrdistance" ascending:YES];
    
    // build new array with thosed elements to be sorted
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    
    // create new array based on the sorted value
    sortedDealers = [unsortedDealers sortedArrayUsingDescriptors:sortDescriptors];  
    
    [self.tableView reloadData];
    
    //error message if there is no data found. Usually this is an error with the connection, 
    //it should never return empty
    if (jsonArray ==  nil) 
    {
        NSLog(@"No Data Loaded. Please check your internet connection");
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UITableView that is populated with custom cells (inherited from UITableViewCell ),
I have a custom UITableView cell that has a UIImageView sized to the width
I have a UITableView whose cells contain custom ImageViews that asynchronously load the images
I have a UITableView that displays various nsstrings from a custom object called a
I have a UITableView populated with custom UITableViewCells. Within those custom cells, I have
I have a custom UITableView that allows the user to select multiple images. The
I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form)
I have a UITableView that gets populated via CoreData , and have just noticed
I have a Custom Section Header in UITableView , on that section header there
I have created a custom UITableView and I want to add it to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.