如何比较If循环中的不同字符串?

时间:2016-11-10 06:43:07

标签: objective-c nsstring comparison

如何比较两个不同格式字符串的字符串?例如,在下面的代码中:

str1 = [dataDic1 objectForKey:[finalArray objectAtIndex:indexPath.row]];
str1 contains 124.00,120/70-14,1,759,140/70-14,48.8 x 57.0.

str2 = [dataDic2 objectForKey:[finalArray objectAtIndex:indexPath.row]];
str2 contains 1.00,90/90-6,1,250,90/90-6,45.3 x 87.0.

我想比较str1str2

if ([bike1Str intValue] < [bike2Str intValue]){
      NSLog(@"%@", str2);
 }
else{

 }

例如:if (120/70-14 < 90/90-6) 我该如何进行这种比较?

DataDic1 {
"Displacement_trim" = "124.00 ";
"Dry_Weight" = "<null>";
"Front_Brakes_Size_trim" = "260 ";
"Front_Tire_Size" = "120/70-14";
"Fuel_Capacity_trim" = "13.50 ";
"Overall_Height_trim" = "1,759 ";
"Overall_Length_trim" = "2,230 ";
Power = "";
"Power_Weight_Ratio" = "<null>";
"Rear_Brake_Size_trim" = "240 ";
"Rear_Tire_Size" = "140/70-14";
Stroke = "";
"Torque_trim" = "";
"stroke_trim" = "48.8 x 57.0 ";
}

和finalArray

(
 Power_Weight_Ratio,
 Rear_Brake_Size_trim,
 Dry_Weight,
 Torque_trim,
 stroke_trim,
 Rear_Tire_Size,
 Front_Brakes_Size_trim,
 Fuel_Capacity_trim,
 Overall_Length_trim,
 Front_Tire_Size,
 Stroke,
 Power,
 Displacement_trim,
 Overall_Height_trim
 )

不仅浮动值我要求所有要比较的值

1 个答案:

答案 0 :(得分:0)

NSMutableDictionary *dict1,*dict2;
NSMutableArray *ArrayContaingFloat;

dict1 = [[NSMutableDictionary alloc]init];
dict2 = [[NSMutableDictionary alloc]init];

ArrayContaingFloat = [[NSMutableArray alloc]init];

[dict1 setObject:[NSNumber numberWithFloat:14.20] forKey:@"Displacement_trim"];

[dict2 setObject:[NSNumber numberWithFloat:20.20] forKey:@"Displacement_trim"];

if ( [[dict1 valueForKey:@"Displacement_trim"] compare:[dict2 valueForKey:@"Displacement_trim"]]==NSOrderedAscending) {
    NSLog(@"dict 2 is greater");
}else{
    NSLog(@"dict 1 is greater");
}

if( [[dict1 valueForKey:@"Displacement_trim"] compare:[dict2 valueForKey:@"Displacement_trim"]]==NSOrderedAscending){

}

[ArrayContaingFloat addObject:[dict1 valueForKey:@"Displacement_trim"]];
[ArrayContaingFloat addObject:[dict2 valueForKey:@"Displacement_trim"]];

NSLog(@"Array conting float %@",ArrayContaingFloat);

NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];

[ArrayContaingFloat sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];
NSLog(@"After Sorting %@",ArrayContaingFloat);

你会得到这样的输出:

dict 2 is greater
Array conting float (
    "14.2",
    "20.2"
)
 After Sorting (
    "20.2",
    "14.2"
)

如果您有任何进一步的疑问,请与我们联系。