Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I Need to Sort out an array of custom objects.

The object is simple, it stores an x and y co-ordinate, for example:

XYPointObject.xCoordinate = [NSNumber];
XYPointObject.yCoordinate = [NSNumber];

I have an array that stores up to 676 of these objects that I need to sort into a numerical order as in x values in numerical order, and the y values connected to the x values in order as well. for example, if the input co-ordinates are:

23,5 | 
5,7 | 
1,4 | 
1,7 | 
21,8 | 
9,12 | 
16,19

the sorted array would have the order

1,4 | 1,7 | 5,7 | 9,12 | 16,19 | 23,5 

keep in mind the max x,y co-ordinants are 25 (25,25)

share|improve this question

2 Answers

Did you even take a look at the documentation?

Here's the method that you need:

-[NSArray sortedArrayUsingComparator:]
share|improve this answer

Use:

NSSortDescriptor *xSorter = [[NSSortDescriptor alloc] initWithKey:@"xCoordinate" ascending:YES];
NSSortDescriptor *ySorter = [[NSSortDescriptor alloc] initWithKey:@"yCoordinate" ascending:YES];
NSArray *sortedArray=[array sortedArrayUsingDescriptors:@[xSorter,ySorter]];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.