0

Hi I'm new with the Objective-C and i try to do some try. I have an NSArray called "values". It is an array of array. It seems like :

["0" = > "aString",6872,5523,0091]

["1" = > "anotherString",4422,1234,0091]

["2" = > "aString",6812,2143,0314] ...

How do I sort the "values" array than the first integer value? I should use the NSPredicate ? please help me with some example. thanks

1 Answer 1

3

Something like that with block (assuming that your integer value are NSNumber or some class that can be compared):

NSArray *sortArray = [yourArray sortedArrayUsingComparator: ^(id elt1, id elt2) {
    return [[elt1 objectAtIndex:1] compare:[elt2 objectAtIndex:1]];
    } ];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution. I had found another one using this method static NSInteger intSort(id num1, id num2, void *context) { int v1 = [[num1 objectAtIndex:1]intValue]; int v2 = [[num2 objectAtIndex:1]intValue]; if (v1 < v2) return NSOrderedDescending; else if (v1 > v2) return NSOrderedAscending; else return NSOrderedSame; } and this declaration : [values sortedArrayUsingFunction:intSort context:NULL] where values are the NSArray with the object to sort.

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.