0

Possible Duplicate:
How to sort an NSMutableArray with custom objects in it?

i have a object with Type of NSObject contains "posts" object type of NSMutableArray. that post object has ID property.

MyObject.posts.post.ID = NSInteger;

I want to sort "posts" NSMutableArray by ID. i have tried couple ways but couldn't be success. any help/sample would be great.

thank you.

1
  • 3
    Could you please post some code of the things you tried? It is usually much easier to pinpoint problems in your code than to write something up from scratch. Commented Dec 4, 2011 at 19:27

2 Answers 2

1

You can implement a compare method, or use NSSortDescriptor.

This question and the answer may helps you.

How to sort an NSMutableArray with custom objects in it?

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

1 Comment

Please don't simply post links to other questions as answers. Leave a comment and flag this question as a duplicate.
1

You can sort an array with a block using NSComparator (NSComparator)

Definition of -sortedArrayUsingComparator: found here

MyObject.posts = [MyObject.posts sortedArrayUsingComparator:^(id obj1, id obj2) {
  if (obj1.post.ID > obj2.post.ID)
    return (NSComparisonResult)NSOrderedDescending;
  if (obj1.post.ID < obj2.post.ID)
    return (NSComparisonResult)NSOrderedAscending;
  return (NSComparisonResult)NSOrderedSame;
}];

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.