[英]sorting nsmutablearray containing dictionary in alphanumeric way
I have array containing dictionary like below: 我有包含字典的数组,如下所示:
(
{
"act_priority" = B1;
}
{
"act_priority" = "";
}
{
"act_priority" = A;
}
{
"act_priority" = A3;
}
{
"act_priority" = B;
}
{
"act_priority" = A2;
}
{
"act_priority" = A1;
}
{
"act_priority" = "";
}
)
I would like to sort in alphabetically and numerically both way: 我想按字母和数字两种方式排序:
(
{
"act_priority" = A1;
}
{
"act_priority" = A2;
}
{
"act_priority" = A3;
}
{
"act_priority" = B1;
}
{
"act_priority" = A;
}
{
"act_priority" = B;
}
{
"act_priority" = "";
}
{
"act_priority" = "";
}
)
What i had tried is below: 我试过的是下面:
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];
}];
Also: 也:
NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"act_priority" ascending:YES];
[activityArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
but it give me like 但它给我像
(
{
"act_priority" = "";
}
{
"act_priority" = "";
}
{
"act_priority" = A;
}
{
"act_priority" = B;
}
{
"act_priority" = A1;
}
{
"act_priority" = A2;
}
{
"act_priority" = A3;
}
{
"act_priority" = B1;
}
)
Here is what I think you want. 这就是我想你想要的。 I gave also an sample to test. 我还提供了一个样本进行测试。
The main point is what you do inside the block. 要点是您在块内执行的操作。
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);
With this your "special" method: 使用此“特殊”方法:
-(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;
}
Output: 输出:
>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.