![](/img/trans.png)
[英]Sorting NSMutableArray that contains NSMutableDictionary by the dictionary key's value
[英]sorting nsmutablearray containing dictionary in alphanumeric way
我有包含字典的數組,如下所示:
(
{
"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;
}
)
這就是我想你想要的。 我還提供了一個樣本進行測試。
要點是您在塊內執行的操作。
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:@"0123456789"]; //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" = "";
} )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.