0

I have a mutable Arra of objects of a class "Place".

I want to sort it on the basis of an attribute of objects "Place Name"..

so i saw some links...but not able to write proper code...

I am using

-[NSMutableArray sortUsingSelector:]

can i get some example of how to use this -[NSMutableArray sortUsingSelector:]

pls help me out

Thanks

1

2 Answers 2

1

I would use a NSSortDescriptor:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCompare:)];
[mutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

Im the NSSortDescriptor is call the localizedCompare: on NSString, I assume that the name property is a NSString.

Sign up to request clarification or add additional context in comments.

Comments

0

If you need the case insensitive compare then use,

NSSortDescriptor *sorter = [[[NSSortDescriptor alloc]
          initWithKey:@"Place Name"
          ascending:YES
          selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[yourObjectArrayt sortUsingDescriptors:sortDescriptors]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.