2

I have a txt file (string) with elements separated with ";". I an reading this into an MSMutable array. I need to sort on the field following the date. It will be integer data. How do I grab this field out of the string in order to sort it. I have searched for days and cannot find a reference to this.

2012/09/17;5;-54.74 2012/09/17;76;6.53 2012/09/17;66;6.53 2012/09/17;69;6.53 2012/09/17;60;6.53 2012/09/17;96;6.53 2012/09/17;86;6.53 2012/09/17;77;6.53

Thanks,

Ron

PermGenError
  • 45,977
  • 8
  • 87
  • 106
Ron Bledsoe
  • 161
  • 1
  • 2
  • 8

1 Answers1

1

You can use the sortUsingComparator: method, like this:

[array sortUsingComparator: ^(id lhs, id rhs) {
    // Get the string between the first and the second semicolons:
    NSString *obj1 = [[lhs componentsSeparatedByString:@";"] objectAtIndex:1];
    NSString *obj2 = [[rhs componentsSeparatedByString:@";"] objectAtIndex:1];
    // Compare the two strings as integers:
    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523