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.

Community
  • 1
  • 1
modusCell
  • 13,151
  • 9
  • 53
  • 80
  • 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. – Sergey Kalinichenko Dec 04 '11 at 19:27

2 Answers2

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?

Community
  • 1
  • 1
virushuo
  • 660
  • 3
  • 5
  • Please don't simply post links to other questions as answers. Leave a comment and flag this question as a duplicate. – jscs Dec 04 '11 at 23:16
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;
}];
DanZimm
  • 2,528
  • 2
  • 19
  • 27