NSMutableArray sort with object
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.
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?
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;
}];
链接地址: http://www.djcxy.com/p/70830.html
上一篇: 根据NSNumber对字典数组进行排序
下一篇: NSMutableArray与对象排序