gpt4 book ai didi

ios - 以字母数字方式对包含字典的 nsmutablearray 进行排序

转载 作者:行者123 更新时间:2023-11-29 02:18:49 26 4
gpt4 key购买 nike

我有一个包含如下字典的数组:

(
{
"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;

}
)

最佳答案

这就是我认为你想要的。我还提供了一个样本来测试。

要点是你在 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);

用这个你的“特殊”方法:

-(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" = "";
} )

关于ios - 以字母数字方式对包含字典的 nsmutablearray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com