xcode plist sorting問題

本帖最後由 ahead_lam 於 2015-7-6 22:25 編輯

咁多位ching 小弟整個dictionary 既plist出黎
比如以下咁
key                     value
ccc                     20
abc                       23
bac                      21
aaa                     34


我讀晒成個list個陣 佢自動幫我sort倣個key 由a-z咁排
想請問可唔可以唔用將條list再重新讀入一個array重新自己做sort既情況下
可以由value既最大排最頭 之後一路落去
小弟用緊OBJECTIVE C

回覆 1# ahead_lam


   NSDictionary does not have order, only NSArray has order.

Here's what you can do:

    NSDictionary *d = @{@"ccc":@20,
                        @"abc":@23,
                        @"bac":@21,
                        @"aaa":@34};
    NSArray *sortedKeys = [d keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj2 compare:obj1];
    }];
    [sortedKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *key = obj;
        NSNumber *value = d[obj];
        NSLog(@"Key %@, Value %ld",key,[value integerValue]);
    }];

TOP

回覆  ahead_lam


   NSDictionary does not have order, only NSArray has order.

Here's what you can ...
virtex 發表於 2015-7-6 23:57



所以我個PLIST要轉做ARRAY?

TOP

回覆 3# ahead_lam


    NSDictionary / NSArray / NSSet are different things for different purpose.  It's not hard to convert between them.  but one of them should be most suitable for the job.  You need to understand your purpose to choose.

TOP

本帖最後由 ahead_lam 於 2015-7-8 09:20 編輯
回覆  ahead_lam


    NSDictionary / NSArray / NSSet are different things for different purpose.  I ...
virtex 發表於 2015-7-7 00:24



  想做個類似排名榜既野

TOP