User558 User558 - 29 days ago 14
Objective-C Question

How do you compare different strings in an If loop?

How do you compare two strings with different format strings? For example, in the code below:

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.


I want to compare
str1
and
str2


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

}


For example:
if (120/70-14 < 90/90-6)

How do I do this type comparison ?

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 ";
}


and 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
)


Not only float values i am asking all the all values to compare

Answer
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);

you will get output like this:

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

let me know if you have any further query.