-2

I have two strings, one contains the value "5.2.3" and another one contain a value like "5.2.32". My question is: how to compare these two strings?

if ([string1 integerValue] >= [sting2 integerValue]) 
{
            NSLog(@"process");
 }

I tried above line but not got it.

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
dhaya
  • 1,522
  • 13
  • 21

3 Answers3

4

Well correct answer has been already given. Because I have spent my half an hour on it so I don't want to waste it.

-(BOOL)string:(NSString*)str1 isGreaterThanString:(NSString*)str2
{
    NSArray *a1 = [str1 componentsSeparatedByString:@"."];
    NSArray *a2 = [str2 componentsSeparatedByString:@"."];

    NSInteger totalCount = ([a1 count] < [a2 count]) ? [a1 count] : [a2 count];
    NSInteger checkCount = 0;

    while (checkCount < totalCount)
    {
        if([a1[checkCount] integerValue] < [a2[checkCount] integerValue])
        {
            return NO;
        }
        else if([a1[checkCount] integerValue] > [a2[checkCount] integerValue])
        {
            return YES;
        }
        else
        {
            checkCount++;
        }
    }

    return NO;
}

And you can call this method like this:-

if([self string:str1 isGreaterThanString:str2])
{
    NSLog(@"str2 is lower than the str1");
}
else
{
     NSLog(@"str1 is lower than the str2");
}
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • I would consider `"5.30.2" > "5.3.2"` the correct result. Why should these be equal? – Martin R Jun 29 '13 at 07:03
  • Because after decimal point .3000 or .3 be equal. – TheTiger Jun 29 '13 at 07:04
  • Yes, but even if the question title mentions "float value", "5.30.2" is not a floating point number in any sense. It most probably is a version number like "major.minor.build". – Martin R Jun 29 '13 at 07:06
  • Oh YES .... Thanks !! Was missing the point :( – TheTiger Jun 29 '13 at 07:08
  • If you replace `return NO` in your code by `return ([a1 count] > [a2 count])` then it will also recognise that `"1.2.3" > "1.2"` :-) – Martin R Jun 29 '13 at 08:20
  • But what if **str1** is **1.1.1** and **str2** is **1.2** ... In this case `return([a1 count] > [a2 count])` will return `YES` while it is `FALSE` according to question :) – TheTiger Jun 29 '13 at 09:03
  • No, the method will return NO already in the while-loop. – Martin R Jun 29 '13 at 09:07
  • hmmm ... Well last `return` is just because of method is `BOOL` type else this doesn't matter here, It is when both strings be exact equal. – TheTiger Jun 29 '13 at 09:17
2

It would appear that what you have here are not really "float" values, but some kind of multi-part "number" (akin to software version numbering?) that is not going to be covered by any of the standard conversions, but will also not compare "correctly" as just simple strings.

First you need to specify exactly what your comparison rules are. For example, I suspect you want something like:

   1.2 > 1.1
   1.1.1 > 1.1
   1.11 > 1.2
   1.2.3 > 1.2.2
   1.2.22 > 1.2.3

(in other words, split the string up by "."s, and do a numeric comparison on each component). You'll have to decide how you want to handle things like letters, other delimiters, etc. showing up in the input. For example is 1.0b1 > 1.01 ?

Once you settle on the rules, write a method (returning NSComparisonResult) to implement the comparison. If you want to get fancy, you can even define your comparison method in a category on NSString, so you could do things like

  if ([string1 mySuperDuperCompareTo:string2] == NSOrderedAscending) {
            NSLog(@"%@ < %@", string1, string2);
   }  // ... etc ...

see also How to let the sortedArrayUsingSelector using integer to sort instead of String?

Community
  • 1
  • 1
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • 1.11 > 1.2 .. I think it should be wrong. because .2 is .20 and it is greater than .11 so 1.11 is smaller than 1.20. – TheTiger Jun 29 '13 at 06:15
  • @The Tiger -- It depends; that's why the first thing the OP needs to do is define his rules. If 1.11 and 1.2 are real numbers, then sure, 1.2 is the same as 1.20, so 1.11 < 1.2. But, if they are major/minor version numbers, then it's likely that the sequence goes 1.1, 1.2, 1.3, 1.4 ... 1.9, 1.10, 1.11 – David Gelhar Jun 29 '13 at 06:28
  • This will happen in case of string. In string 1.11 comes after 1.2 – TheTiger Jun 29 '13 at 06:31
0

@The Tiger is correct. Sorry to misunderstood your question. I have already mark deleted as my older answer. Here is the updated one.

As there are multiple . (dots) available here is the new solution. This will check value first like 5.2.3 and 5.2.32 are there. Then,

  • check first value 5 - same
  • so check next 2 - same
  • check next 3 and 32 - 32 is larger
  • also check for the same string as well (Also one of the probability)

Here is the logic - I have not compiled but this is base idea - there might require some correction

// separate from "."

    NSArray *arrString1 = [string1 componentSeparatedBy:@"."];  
    NSArray *arrString2 = [string1 componentSeparatedBy:@"."];

    BOOL isString1Bigger = NO; // a variable to check
    BOOL isString2Bigger = NO; // a variable to check

// check count to run loop accordingly

    if ([arrString1 count] <= [arrString2 count]) {


       for (int strVal=0; strVal<[arrString1 count]; strVal++) {

// compare strings value converted into integer format
// when you get larger then break the loop
          if([[arrString1 objectAtIndex:strVal] intValue] > [[arrString2 objectAtIndex:strVal] intValue]) {

             isString1Bigger = YES;
             break; 

       }
    }
    if ([arrString1 count] > [arrString2 count]) {

    // use arrString2 in loop and isString2Bigger as a mark
    }

// if after running both the if condition still require to check if both are same or not, for that,

if ((isString1Bigger == NO) && (isString2Bigger  == NO)) {

// same string values
}

There might some modification required to run over. But its the base concept to compare string value provided by you.

Mrunal
  • 13,982
  • 6
  • 52
  • 96