1

私はNSMutableArray70個のオブジェクトを持っNSMutableDictionaryています. NSMutableArray小さいものから大きいものまで並べ替えたい。距離は KM で、ここに貼り付けたコードを使用すると、配列は次のようになります。

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[branchDistance sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

これは、このメソッドの結果です:

(lldb) po [branchDistance objectAtIndex:0]
(id) $1 = 0x0d6ac240 {
    "<NSIndexPath 0xd6cf180> 1 indexes [0]" = indexPath;
    "8.078547" = distance;
}
(lldb) po [branchDistance objectAtIndex:1]
(id) $2 = 0x0d6a64a0 {
    "<NSIndexPath 0xd6cf3b0> 1 indexes [1]" = indexPath;
    "45.044069" = distance;
}
(lldb) po [bran(lldb) po [branchDistance objectAtIndex:2]
(id) $4 = 0x0d6cf550 {
    "<NSIndexPath 0xd69b3f0> 1 indexes [2]" = indexPath;
    "9.992081" = distance;
}
(lldb) po [branchDistance objectAtIndex:3]
(id) $5 = 0x0d6cf750 {
    "283.356727" = distance;
    "<NSIndexPath 0xd6cf480> 1 indexes [3]" = indexPath;
}

NSMutableArray内のこの「距離」キー値を使用して、小さな数字から大きな数字に並べ替えたいと思いますNSMutableDictionary。Web と stackoverflow からいくつかのコードを試しましたが、うまくいくものを見つけることができませんでした。

私を助けてください!

ありがとう!

4

1 に答える 1

5

NSNumberオブジェクトを使用して距離値が辞書に格納されていると仮定します。

NSArray *sortedArray = [branchDistance sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSNumber *distance1 = [(NSDictionary *)obj1 objectForKey:@"distance"];
    NSNumber *distance2 = [(NSDictionary *)obj2 objectForKey:@"distance"];
    return [distance1 compare:distance2];
}];
于 2012-11-08T10:36:53.703 に答える