gpt4 book ai didi

ios - 如何对NSString中的成分进行排序?例如1/2杯牛奶,1/4勺粉

转载 作者:行者123 更新时间:2023-12-01 18:07:09 26 4
gpt4 key购买 nike

我在NSArray中将如下成分列表作为NSString对象。

"1/2 Cup Milk",
"125 grams Cashew",
"2 green onions",
"1/4 Cup Sugar",
"1/6 Spoon Salt",
"3/2 XYZ",
"One cup water",
"Almond oil"

我想像下面这样对它们进行排序。
"1/6 Spoon Salt",
"1/4 Cup Sugar",
"1/2 Cup Milk",
"3/2 XYZ",
"2 green onions",
"125 grams Cashew",
"Almond oil",
"One cup water",

尝试1:使用localizedStandardCompare和NSNumericSearch进行排序,两个结果相同。

结果:
"1/2 Cup Milk",
"1/4 Cup Sugar",
"1/6 Spoon Salt",
"2 green onions",
"3/2 XYZ",
"125 grams Cashew",
"Almond oil",
"One cup water"

我知道这是可能的,但我无法以某种方式解决这个问题。

如果有人做过类似的事情,您可以指导我。

提前致谢。

最佳答案

您可以在NSString上定义一个类别以比较数字(包括分数):

@interface NSString (Number)
- (NSNumber * _Nullable)numberValue;
- (NSComparisonResult)compareNumber:(NSString *)string;
@end

@implementation NSString (Number)

- (NSNumber *)numberValue {
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*(\\d+)\\s*/\\s*(\\d+)" options:0 error:&error];
NSAssert(regex, @"%@", error.localizedDescription);

NSTextCheckingResult *match = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (match) {
float numerator = [[self substringWithRange:[match rangeAtIndex:1]] floatValue];
float denominator = [[self substringWithRange:[match rangeAtIndex:2]] floatValue];
return denominator ? @(numerator / denominator) : nil;
}

regex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*(\\d+)" options:0 error:&error];
match = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (match) {
return @([self floatValue]);
}

// if you don't want it to recognize spelt out numbers, comment the following bit of code

regex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*(\\S+)" options:0 error:&error];
match = [regex firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
if (match) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
return [formatter numberFromString:[[self substringWithRange:[match rangeAtIndex:1]] lowercaseString]];
}

return nil;
}

- (NSComparisonResult)compareNumber:(NSString *)string {
NSNumber *number1 = [self numberValue];
NSNumber *number2 = [string numberValue];

if (number1 && !number2) {
return NSOrderedAscending;
}

if (number2 && !number1) {
return NSOrderedDescending;
}

if (number1 && number2) {
NSComparisonResult numericComparison = [number1 compare:number2];
if (numericComparison != NSOrderedSame) {
return numericComparison;
}
}

return [self caseInsensitiveCompare:string];
}

@end

然后,您可以像这样对成分进行排序:
NSArray *ingredients = @[@"1/2 Cup Milk",
@"125 grams Cashew",
@"2 green onions",
@"1/4 Cup Sugar",
@"1/6 Spoon Salt",
@"3/2 XYZ",
@"One cup water",
@"Almond oil"];

NSArray *sorted = [ingredients sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString * _Nonnull obj2) {
return [obj1 compareNumber:obj2];
}];

产生:
"1/6 Spoon Salt",
"1/4 Cup Sugar",
"1/2 Cup Milk",
"One cup water",
"3/2 XYZ",
"2 green onions",
"125 grams Cashew",
"Almond oil"

我必须承认拼写数字的 NSNumberFormatter逻辑不是很健壮(它可以识别“三十二”,但不能识别“三十二”),但是您可以根据需要使用它。或者,您可能会完全拉扯它。

关于ios - 如何对NSString中的成分进行排序?例如1/2杯牛奶,1/4勺粉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838470/

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