2015-02-09 11 views
0

contaning字典我有一個包含字典像下面陣列: -排序的NSMutableArray在alphnumberic方式

(
{ 
    "act_priority" = B1; 
} 
{ 
    "act_priority" = ""; 

} 
{ 
    "act_priority" = A; 

} 
{ 
    "act_priority" = A3; 

} 
{ 
    "act_priority" = B; 

} 
{ 
    "act_priority" = A2; 

} 
{ 
    "act_priority" = A1; 

} 
{ 
    "act_priority" = ""; 

} 
) 

我想按字母順序排序在和數字兩種方式: -

(
    { 
     "act_priority" = A1; 
    } 
    { 
     "act_priority" = A2; 

    } 
    { 
     "act_priority" = A3; 

    } 
    { 
     "act_priority" = B1; 

    } 
    { 
     "act_priority" = A; 

    } 
    { 
     "act_priority" = B; 

    } 
    { 
     "act_priority" = ""; 

    } 
    { 
     "act_priority" = ""; 

    } 
    ) 

我曾試圖低於: -

NSArray* sorted = [activityArray sortedArrayUsingComparator:(NSComparator)^(NSDictionary *item1, NSDictionary *item2) { 
      NSString *score1 = [item1 objectForKey:@"act_priority"]; 
      NSString *score2 = [item2 objectForKey:@"act_priority"]; 
      return [score1 compare:score2 options:NSNumericSearch]; 
     }]; 

另外: -

NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"act_priority" ascending:YES]; 
     [activityArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]]; 

,但它給我像

(

     { 
      "act_priority" = ""; 

     } 
     { 
      "act_priority" = ""; 

     } 
     { 
      "act_priority" = A; 

     } 
     { 
      "act_priority" = B; 

     } 
     { 
      "act_priority" = A1; 

     } 
     { 
      "act_priority" = A2; 

     } 
     { 
      "act_priority" = A3; 

     } 
     { 
      "act_priority" = B1; 

     } 
     ) 

請幫助

+0

在你的塊,檢查字符串的長度(@ 「」),然後返回足夠NSCompareResult (最後用「@」表示)。爲什麼「NSNumericSearch」? – Larme 2015-02-09 12:56:20

+0

@Larme感謝您的快速回復。你能爲我輸入代碼嗎?數字意味着像A1,A2,B1,B2,A,B,「」,「」我喜歡這樣的序列 – 2015-02-09 13:01:22

+0

數字搜索實際上意味着你收到的輸出。如果你想要一個不同的搜索,你將不得不明確定義塊中的規則,而不是使用內置的「比較」。 – 2015-02-09 13:05:28

回答

1

這是我想你想要的。我也給了一個樣本來測試。

重點是你在塊內做什麼。

NSArray *array = @[@{@"act_priority":@"B1"}, 
        @{@"act_priority":@""}, 
        @{@"act_priority":@"A"}, 
        @{@"act_priority":@"A3"}, 
        @{@"act_priority":@"B"}, 
        @{@"act_priority":@"A2"}, 
        @{@"act_priority":@"A1"}, 
        @{@"act_priority":@""}]; 

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) 
{ 
    NSString *string1 = [obj1 objectForKey:@"act_priority"]; 
    NSString *string2 = [obj2 objectForKey:@"act_priority"]; 
    if ([string1 length] == 0)   //To put the @"" at the end 
     return NSOrderedDescending; 
    else if ([string2 length] == 0)  //To put the @"" at the end 
     return NSOrderedAscending; 
    else 
    { 

     BOOL string1HasSuffixNumber = [self hasSuffixNumber:string1]; //If string1 has a number at the end 
     BOOL string2HasSuffixNumber = [self hasSuffixNumber:string2]; //If string2 has a number at the end 
     if (string1HasSuffixNumber && !string2HasSuffixNumber) 
      return NSOrderedAscending; //Put the string2 at the end 
     else if (!string1HasSuffixNumber && string2HasSuffixNumber) 
      return NSOrderedDescending; //Put the string1 at the end 
     else 
      return [string1 compare:string2 options:NSCaseInsensitiveSearch]; //Other option can be used, if you want case sensitive one for example, or not, etc. 
    } 
}]; 

NSLog(@"SortedArray: %@", sortedArray); 

有了這個你的 「特殊」 的方法:

-(BOOL)hasSuffixNumber:(NSString *)string 
{ 
    if ([string length] < 1) 
     return FALSE; //"Security added, but in your use case you shouldn't go there; 

    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@""]; //Explicit the character set in case you want to add some changes 
    if ([[string substringFromIndex:[string length]-1] rangeOfCharacterFromSet:set].location != NSNotFound) 
     return TRUE; 
    else 
     return FALSE; 
} 

輸出:

>SortedArray: (
     { 
     "act_priority" = A1; 
    }, 
     { 
     "act_priority" = A2; 
    }, 
     { 
     "act_priority" = A3; 
    }, 
     { 
     "act_priority" = B1; 
    }, 
     { 
     "act_priority" = A; 
    }, 
     { 
     "act_priority" = B; 
    }, 
     { 
     "act_priority" = ""; 
    }, 
     { 
     "act_priority" = ""; 
    }) 
+0

感謝您的答覆。這個工作! :) – 2015-02-10 05:21:44