How to use compare on a version number where theres less parts in one number in Objective-C?

前端 未结 9 608
感情败类
感情败类 2021-01-22 02:30

I\'ve found the following code at http://snipplr.com/view/2771

Which is pretty good, almost exactly what I was looking for, but if I use the values @\"1.4.5\", @

9条回答
  •  一个人的身影
    2021-01-22 03:05

    I'm not 100% sure what you're asking, but if you're looking to sort numbers regardless of how many periods "." are in the number you can use an NSSortDescriptor on a dictionary of OS versions:

    NSArray *originalArray = [NSArray arrayWithObjects:
                  [NSDictionary dictionaryWithObject:@"10.4.5" forKey:@"version"],
                  [NSDictionary dictionaryWithObject:@"10.5.6" forKey:@"version"],
                  [NSDictionary dictionaryWithObject:@"10.6.8" forKey:@"version"],
                  [NSDictionary dictionaryWithObject:@"10.8" forKey:@"version"],
                  [NSDictionary dictionaryWithObject:@"10.7.1" forKey:@"version"],
                  [NSDictionary dictionaryWithObject:@"10.8.2" forKey:@"version"],
                  nil];
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"version" ascending:true];
    NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    NSLog(@"Lowest to highest: %@", sortedArray);
    NSLog(@"Highest OS version: %@",[[sortedArray objectAtIndex:[sortedArray indexOfObject:[sortedArray lastObject]]] objectForKey:@"version"]);
    

提交回复
热议问题