1

I am stuck here...... I have two arrays Arr_title and Arr_distance I get sorted Arr_distance using this method

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

but problem is that i want to sort Arr_title according to index of sortedFloats array...

thx in advance for help me..........

1
  • what the mean of sortedFloats arry Commented Dec 7, 2012 at 6:08

4 Answers 4

1

Try this,

NSDictionary *dict = [NSDictionary dictionaryWithObjects:Arr_title forKeys:Arr_distance];

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES] autorelease];
sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                         [NSArray arrayWithObject:sortDescriptor]];


NSMutableArray *sortedTitles = [NSMutableArray array];

for (NSString *key in sortedFloats) {//or just use sortedTitles = [dict objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];
    [sortedTitles addObject:[dict valueForKey:key]]; 
}

NSLog(@"%@",sortedValues);

sortedTitles should give you the array sorted in the same order as sortedFloats.

Sign up to request clarification or add additional context in comments.

Comments

1

hey instead of taking two different array take 1 dictionary with key title,distance save that dictionary in array then sort that array So you have got combination together so there is no any problem due to mapping in any sort(by title & by distance).

Comments

0

if i understand your question right , you can use NSDictionary for the same task

NSMutableArray *array = [NSMutableArray arrayWithArray:@[ @{@"title" : @"City1",@"dist" : @5},

                             @ {@"title" : @"City2",@"dist" : @2.3 },
                             @{@"title" : @"City3",@"dist" : @11.0 },
                             @{@"title" : @"City4",@"dist" : @1.0},
                             @{@"title" : @"City5",@"dist" : @.5},
                             @{@"title" : @"City6",@"dist" : @13 }]];

NSLog(@"dict<%@>",array);

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dist" ascending:YES];

NSArray *sortarray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];

NSLog(@"sort array = <%@>",sortarray);

Comments

0

I got the solution see following code.........

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]autorelease];

sortedFloats = [appDel.Arr_distance sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:sortDescriptor]];

 NSLog(@" Sorted Array : %@", sortedFloats);

 NSDictionary *dictTitle = [NSDictionary dictionaryWithObjects:Arr_title   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedTitle = [dictTitle objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Title : %@",sortedTitle);


// Put the two arrays into a dictionary as keys and values
NSDictionary *dictAddress = [NSDictionary dictionaryWithObjects:Arr_address   forKeys:appDel.Arr_distance];

// Sort the second array based on the sorted first array
sortedAddress = [dictAddress objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];


NSLog(@" Sorted Address : %@",sortedAddress);

Dont forgot to retain your arrays if use in another method ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.