2012-12-07 5 views
1

Je suis coincé ici ...... tableau J'ai deux tableaux Arr_title et Arr_distance je suis triés Arr_distance utilisant cette méthodeTrie un tableau avec index

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

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

mais le problème est que je veux trier Arr_title selon l'indice du tableau sortedFloats ...

thx à l'avance de l'aide me ..........

+0

ce que la moyenne de triedFloats arry – Rajneesh071

Répondre

1

Essayez ceci,

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 devrait vous donner le tableau trié dans le même ordre que sortedFloats.

1

hey au lieu de prendre deux tableau différent prendre 1 dictionnaire avec clétitle,distance enregistrer ce dictionnaire dans le tableau, puis trier ce tableau Donc, vous avez une combinaison de sorte qu'il n'y a aucun problème en raison de la cartographie de toute sorte (par le titre & par la distance).

+0

Je l'ai eu Sir merci d'aider son travail pour moi ....... –

0

si je comprends votre question à droite, vous pouvez utiliser NSDictionary pour la même tâche

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); 
0

Je suis la solution voir le code suivant .........

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 oublié de conserver vos tableaux si vous utilisez dans une autre méthode ....

Questions connexes