0

i have created an application which show data from user current location. I want to sort them in assending order on the base of minimum distance found of the user. I know that i need to use this kind of function:

[userLocationd istanceFromLocation:cameraLocation];

But, what do i do after that? I should sort everyone after distance and putting them in an array, but how do i rearrange every cell then according to minimum location get of the user? (cellForRowAtIndexPath)

Also; i havent really figured out how to find the users location. I am on that, but it would be helpful if someone pointed me in the right direction.

How to sort the array to min distance to maximum location?

i have used this code.

    CLLocation *location1 = [[[CLLocation alloc] initWithLatitude:someLatitude longitude:someLongitude] autorelease];

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

    float target = [location2 distanceFromLocation:location1];
    target = target / 1000;

Thanks in advance!

Hrushikesh Betai
  • 2,227
  • 5
  • 33
  • 63

2 Answers2

0

Sort the array that you have in your model so that it is in the distance order you want and then call a reload on the tableview. That will then refresh from the data model and your list will be in the correct order.

Nick Bull
  • 4,276
  • 1
  • 18
  • 25
0

i have found the solution use this code it will work perfect

 uint smallest[5];

    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithDouble:0.5], [NSNumber numberWithDouble:0.25], [NSNumber numberWithDouble:1], [NSNumber numberWithDouble:0.75], [NSNumber numberWithDouble:0.15], [NSNumber numberWithDouble:0.85], [NSNumber numberWithDouble:0.95], [NSNumber numberWithDouble:0.32], [NSNumber numberWithDouble:0.21], [NSNumber numberWithDouble:0.05], nil];

    // declare flags
    uint flags[array.count];

    // initialize flags
    for(uint i = 0; i < [array count]; i++)
        flags[i] = 0;

    for(int i = 0; i < 5; i++)
    {
        smallest[i] = -1;
        for(int j = 0; j < [array count]; j++)
        {
            // check flag
            if(!flags[j])
            {
                if(smallest[i] == -1 ||
                   [[array objectAtIndex:smallest[i]] doubleValue] > [[array objectAtIndex:j] doubleValue])
                {
                    // reset flag for previous index
                    if(smallest[i] != -1)
                        flags[smallest[i]] = 0;
                    // set flag for current index
                    flags[j] = 1;
                    smallest[i] = j;
                }
            }
        }
    }

    for(uint i = 0; i < 5; i++)
        NSLog(@"Smallest[%u]: %u -> %f", i, smallest[i], [[array objectAtIndex:smallest[i]] doubleValue]);

if anybody have any problem than please link to this question dedicated to original developer MatthewD

Hrushikesh Betai
  • 2,227
  • 5
  • 33
  • 63