NSMutableArray 对对象进行排序

发布于 2024-12-19 11:53:24 字数 542 浏览 1 评论 0原文

可能的重复:
如何对其中包含自定义对象的 NSMutableArray 进行排序?< /a>

我有一个类型为 NSObject 的对象,其中包含 NSMutableArray 的“posts”对象类型。该帖子对象具有 ID 属性。

MyObject.posts.post.ID = NSInteger;

我想按 ID 对“帖子”NSMutableArray 进行排序。我尝试了几种方法但未能成功。 任何帮助/示例都会很棒。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2024-12-26 11:53:24

您可以实现比较方法,或使用 NSSortDescriptor。

这个问题和答案可能对你有帮助。

如何对其中包含自定义对象的 NSMutableArray 进行排序?< /a>

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?

花期渐远 2024-12-26 11:53:24

您可以使用 NSComparator 对带有块的数组进行排序(NSComparator)

-sortedArrayUsingComparator 的定义:找到 此处

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;
}];

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;
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文