0

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

User558
  • 1,165
  • 1
  • 13
  • 37
  • 1
    Try to get values in array not in string. Then its easy to compare by using for loop – Himanth Nov 10 '16 at 06:46
  • @himanth. ok i will try and let u know – User558 Nov 10 '16 at 06:51
  • @himanth i tried with this code not getting plz help – User558 Nov 10 '16 at 07:20
  • Can you show your dataDic1 and finalArray? – Himanth Nov 10 '16 at 07:22
  • for (int i = 0; i < [bike1A count]; i++) { for (int j = 0; j<[bike2A count]; j++) { if ([[array1 objectAtIndex:bike1]isEqualToString:[array2 objectAtIndex:j]]) { cell.NameLbl.text = [array1 objectAtIndex:i]; } else { } } } – User558 Nov 10 '16 at 07:22
  • just go through it http://stackoverflow.com/questions/1614533/strange-problem-comparing-floats-in-objective-c. and it's not string it's float conversion. – Narendra Pandey Nov 10 '16 at 07:34
  • Possible duplicate of [How to compare two dictionary key values and print text in cell in iOS?](http://stackoverflow.com/questions/40506474/how-to-compare-two-dictionary-key-values-and-print-text-in-cell-in-ios) – Narendra Pandey Nov 10 '16 at 07:37
  • @NarendraPandey not only float values i am asking these type of strings 120/70-14 and 48.8 x 57.0 – User558 Nov 10 '16 at 07:48
  • 120/70-14 and 48.8 x 57.0 what you Call this thing float or string. why are you so confused convert this into float, store into array if it's in large amount. Sort it you will get greater or smaller value. – Narendra Pandey Nov 10 '16 at 07:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127773/discussion-between-user558-and-narendra-pandey). – User558 Nov 10 '16 at 08:57

1 Answers1

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

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.

Narendra Pandey
  • 377
  • 9
  • 28