2012-12-07 62 views
1

我堅持在這裏...... 我有兩個數組Arr_title和Arr_distance 我得到整理Arr_distance使用這種方法排序使用數組索引

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

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

但問題陣列是一個我要排序提前幫我根據sortedFloats數組的索引Arr_title ...

THX ..........

+0

排序浮標的意思是什麼 – Rajneesh071

回答

1

試試這個,

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應該給你以同樣的順序爲sortedFloats排序的數組。

1

哎,而不是採取兩種不同的陣列取1個字典,關鍵title,distance在數組中保存該字典,然後對該數組進行排序因此,您已經組合在一起,因此不存在由於任何類型的映射(按標題&距離)的問題。

+0

我知道了先生,謝謝你幫助它爲我工作....... –

0

如果我明白你的問題的權利,你可以使用的NSDictionary爲同一任務

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

我得到了解決看下面的代碼.........

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

不要忘了保留你的陣列,如果在另一種方法中使用... ...