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

1 Answer 1

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;
}];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I am new at this. Let me give it a try
This works great but how do i sort on the date field. I cannot get it to select a date object
@RonBledsoe objectAtIndex:0 will give you the date string. Parse it (link) and compare them for sorting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.