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 7039547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:48:42+00:00 2026-05-28T01:48:42+00:00

I want to sort my tableview cells by nearest location to the user. Let’s

  • 0

I want to sort my tableview cells by nearest location to the user.
Let’s say i have 14 stores & i want to sort it by location.
Is there any example for doing something like this ?

Thanks!


Yes,
I have a tableview with 14 cells & i put names of 14 stores form plist in those cells with navigation control to other views with details (from the plist too).
The 14 stores are all over the country & i want to sort them by place according to my location in the country (with the assistant of the GPS of course)
I want that the neares store will be at the top of the table.

This is my sort code that i need to use but i can’t figure it out how & where to integrate it with the table.
For everyone who need that – feel free to use this code :).. alot of people need that:

- (NSComparisonResult)compareDistance:(id)obj 
{
    CLLocation* loc = [[CLLocation alloc]initWithLatitude:32.066157 longitude:34.777821];

    StoreAnnotation* operand1 = self;
    StoreAnnotation* operand2 = obj;

    CLLocation* operand1Location = [[CLLocation alloc]initWithLatitude:operand1.coordinate.latitude longitude:operand1.coordinate.longitude];

    CLLocationDistance distanceOfLocFromOperand1 = [loc distanceFromLocation:operand1Location];
    NSLog(@"The distance of loc from op1 = %f meters", distanceOfLocFromOperand1);

    CLLocation* operand2Location = [[CLLocation alloc]initWithLatitude:operand2.coordinate.latitude longitude:operand2.coordinate.longitude];

    CLLocationDistance distanceOfLocFromOperand2 = [loc distanceFromLocation:operand2Location];
    NSLog(@"The distance of loc from op2 = %f meters", distanceOfLocFromOperand2);

    if (distanceOfLocFromOperand1 < distanceOfLocFromOperand2) 
    {
        return NSOrderedAscending;
    }

    else if (distanceOfLocFromOperand1 > distanceOfLocFromOperand2)
        return NSOrderedDescending;

    else return NSOrderedSame;

}

It will be great if someone could help me with that..
Thanks alot.

  • 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-05-28T01:48:43+00:00Added an answer on May 28, 2026 at 1:48 am

    OK so i got the code that doing the job with the cells together with the sorting code below.
    I hope it will help to a lot of developers that need that.
    Thanks for trying to help 🙂

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [arrayFromPlist count];
        NSLog(@"Array SIZE = %d",[arrayFromPlist count]);
    }
    
    - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    
    }   
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        StoreAnnotation* tmpSt = [[Storage sharedStorage].sortedStoresArr objectAtIndex:indexPath.row];
        cell.textLabel.text = tmpSt.title;
        cell.textLabel.textColor = [UIColor whiteColor];
        double lat1=[lat doubleValue];
        double lon1=[lon doubleValue];
        CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];
        CLLocation *distForIndex = [[CLLocation alloc] initWithLatitude:tmpSt.coordinate.latitude longitude:tmpSt.coordinate.longitude];
        CLLocationDistance distance = [userLocation distanceFromLocation:distForIndex];
        NSLog(@"DISTANCE FOR CELL %d - %f",indexPath.row, distance);
        cell.detailTextLabel.text = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%0.1f kmh", distance/1000]];
        cell.detailTextLabel.textColor = [UIColor yellowColor];
        cell.imageView.image = [UIImage imageNamed:@"imageName.png"];
        UIView* backgroundView = [[ UIView alloc ] initWithFrame:CGRectZero];
        backgroundView.backgroundColor = [ UIColor blackColor ];
        cell.backgroundView = backgroundView;
        for ( UIView* view in cell.contentView.subviews ) 
        {
            view.backgroundColor = [ UIColor blackColor ];
        }
    
    return cell;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to sort my Data in my Tableview. I have UIButtons with IBAction
I have List I want to sort Desc by Priority, which is int and
I have an array that I want to sort based on the value of
I have a NSTableView with 3 columns. I can sort using any of the
I want to sort a ListView control in C# based on columns. I have
Sort of a simple question. I have a tableview in a navigationcontroller. When I
For the sake of this example, let's say I have a button that every
I want to sort list of strings with respect to user language preference. I
i want to sort dictionary based upon value of each dictioanry item.but if i
I want to sort items where the comparison is performed by humans: Pictures Priority

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.