• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie: compare version string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie: compare version string


  • Subject: Re: Newbie: compare version string
  • From: "Clark Cox" <email@hidden>
  • Date: Sun, 9 Dec 2007 12:36:08 -0800

On Dec 9, 2007 10:39 AM, Adam R. Maxwell <email@hidden> wrote:
>
> On Dec 9, 2007, at 10:27 AM, Clark Cox wrote:
>
> > On Dec 9, 2007 7:10 AM, Fritz Anderson <email@hidden>
> > wrote:
> >> A lexical comparison (-[NSString compare:]) won't work, even without
> >> fourth versions and stage letters. Consider that 10.4.1 < 10.4.11 <
> >> 10.4.2.
> >
> > No, but
> > [string1 compare: string2 options: NSNumericSearch]
> > should work.
>
> At least as of 10.4.3, that didn't work correctly with string
> literals, so we use an NSString category method:
>
> - (NSComparisonResult)numericCompare:(NSString *)otherString{
>      return CFStringCompare((CFStringRef)self,
> (CFStringRef)otherString, kCFCompareNumerically);
> }

I can't speak to 10.4.3, but I just tried it out on 10.5.1, and it
works as expected:

//begin main.m
#import <Cocoa/Cocoa.h>

static NSInteger CompareFunc(id a, id b, void *ignored) {
    return [a compare: b options: NSNumericSearch];
}

int main(int argc, char *argv[])
{
    NSAutoreleasePool   *localPool  = [[NSAutoreleasePool alloc] init];
    NSString *strings[] = {
        @"1",
        @"1.2",
        @"1.5",
        @"1.41",
        @"1.11",
        @"10.2.1",
        @"10.5.11",
        @"10.4.3"
    };


    NSArray *stringArray    = [NSArray arrayWithObjects: strings
                                                  count: sizeof
strings / sizeof *strings];

    NSArray *sortedArray    = [stringArray sortedArrayUsingFunction:
CompareFunc context: NULL];

    NSLog(@"Before:\n%@", stringArray);
    NSLog(@"After:\n%@", sortedArray);
    [localPool release];
    return 0;
}

//end main.m

outputs:
2007-12-09 12:35:21.741 CompareTest[4097:817] Before:
(
    1,
    "1.2",
    "1.5",
    "1.41",
    "1.11",
    "10.2.1",
    "10.5.11",
    "10.4.3"
)
2007-12-09 12:35:21.763 CompareTest[4097:817] After:
(
    1,
    "1.2",
    "1.5",
    "1.11",
    "1.41",
    "10.2.1",
    "10.4.3",
    "10.5.11"
)

--
Clark S. Cox III
email@hidden
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Newbie: compare version string
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >Newbie: compare version string (From: norio <email@hidden>)
 >Re: Newbie: compare version string (From: Fritz Anderson <email@hidden>)
 >Re: Newbie: compare version string (From: "Clark Cox" <email@hidden>)
 >Re: Newbie: compare version string (From: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: Re: IB 3 z-order problem
  • Next by Date: Re: Debugger exits with signal 5 (SIGTRAP) after computer crashed.
  • Previous by thread: Re: Newbie: compare version string
  • Next by thread: Re: Newbie: compare version string
  • Index(es):
    • Date
    • Thread