2012-12-04 70 views
1

我排序我的距離按升序排序使用排序描述符的數組,但有幾個索引沒有保存距離,所以我存儲了一個更大的數字,其中找不到距離幫助我正確排序我的數組。但是如果沒有找到距離,存儲更大的數字並不是一個好方法。因此,我正在尋找一種解決方案,按照升序對我的距離進行排序,找到並找不到它,因此它應該在結尾索引中排序,而不需要在NSArray中編碼更大的數字。我的排序代碼如下。按升序排序NSArray與結尾零

NSLog(@"%@", self.distanceArr); // self.distanceArr is a Mutable Array 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"OnlyDistance" ascending:YES]; 
    NSArray *sort = [self.distanceArr mutableCopy]; 
    NSArray *sortedArray = [sort sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; 

    NSLog(@"Sorted %@",sortedArray); 
    NSLog(@"%d",[sortedArray count]); 

    self.sortedBusinessDetArr = [sortedArray mutableCopy]; 


    [self.searchTbl reloadData]; 

而我的陣列是作爲遵循

(
{ 
    OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found. 
}, 
    { 
    OnlyDistance = "13089.53"; 
}, 
    { 
    OnlyDistance = "12991.81"; 
}, 
{ 
    OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found. 
}, 
{ 
    OnlyDistance = "10000.0"; /// Hard Coded Value where distance is not found. 
}, 
    { 
    OnlyDistance = "13089.53"; 
} 

OnlyDistance是一個NSNumber其中浮點值被存儲。如果我存儲0.0是NSNumber,所以它也排序爲 在上面用零遞增。我想要持有零的值或未找到結果作爲底部排序,只有保存值的索引應位於頂部。我不能按降序排序。

回答

1

做這樣的

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO]; 
[self.distanceArr sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];