如何通过 NSDates 和 NSStrings 对数组重新排序

发布于 2024-11-03 07:11:17 字数 141 浏览 1 评论 0原文

我有一个数组,其中包含我的一个名为“游戏”的自定义类。每个“游戏”都包含一个 NSDate。我想根据日期重新排序我的数组,以便它们按从最新到最旧的顺序排列。我怎么能这么做呢?另外,我的“游戏”类包含一个 NSString。我如何重新排序我的数组以便游戏按字母顺序排列?

I have an array that contains a custom class of mine called 'Game'. Each 'Game' contains an NSDate. I would like to reorder my Array based on the dates so that they are in order from newest to oldest. How could I do that? Also, my class 'Game' contains an NSString. How could I reorder my Array so that the games are alphabetized?

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

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

发布评论

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

评论(2

你怎么敢 2024-11-10 07:11:17

有一种非常简单的方法可以做到这一点,那就是使用 NSSortDescriptor

NSArray *games = ...; //your unsorted array of Game objects
NSSortDescriptor *sortByDateDescending = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:sortByDateDescending];

NSArray *sortedGames = [games sortedArrayUsingDescriptors:descriptors];

Apple 已经编写了排序代码。它只需要知道按哪些属性进行排序。这就是 NSSortDescriptor 所封装的内容。一旦你得到了一个,只需将它交给数组,数组就会返回一个排序版本。 (或者,如果您有 NSMutableArray,则可以使用 -sortUsingDescriptors: 对它进行就地排序)

There's a REALLY simple way to do this, and that's with an NSSortDescriptor:

NSArray *games = ...; //your unsorted array of Game objects
NSSortDescriptor *sortByDateDescending = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:sortByDateDescending];

NSArray *sortedGames = [games sortedArrayUsingDescriptors:descriptors];

Apple's already written the sorting code. It just needs to know what properties to sort by. That's what an NSSortDescriptor encapsulates. Once you've got one, simply give it to the array, and the array give you back a sorted version. (Alternatively, if you have an NSMutableArray, you can sort it in-place using -sortUsingDescriptors:)

生寂 2024-11-10 07:11:17

该数组必须是 NSMutableArray 对象,然后才能重新排序,如果我错了,请纠正我。

为了对数组进行排序,您需要在 Game 类中编写自定义排序方法。例如:

- (NSComparisonResult)compareGameByString:(Game *)otherGame
{ return [[self stringValue] caseInsensitiveCompare:[otherGame stringValue]]; }

然后:

[yourMutableArray sortUsingSelector:@selector(compareGameByString:)];

比较日期:

- (NSComparisonResult)compareByDate:(Game *)otherGame
{
   if( [otherGame isKindOfClass:[Game class]] )
   {
      // NSdate has a convenient compare method
      return [[self dateValue] compare:[otherGame dateValue]];
   }

另请注意,如果您的数组包含不响应这些选择器的对象,因此该对象不是 Game 对象,您将收到异常,并且您的应用程序可能会崩溃

The array must be an NSMutableArray object before you can re-order it, correct me if I am wrong.

In order to sort the array you need to write a custom sort method in the Game class. Something like:

- (NSComparisonResult)compareGameByString:(Game *)otherGame
{ return [[self stringValue] caseInsensitiveCompare:[otherGame stringValue]]; }

Then:

[yourMutableArray sortUsingSelector:@selector(compareGameByString:)];

Comparing dates:

- (NSComparisonResult)compareByDate:(Game *)otherGame
{
   if( [otherGame isKindOfClass:[Game class]] )
   {
      // NSdate has a convenient compare method
      return [[self dateValue] compare:[otherGame dateValue]];
   }

Also note that in the event of your array containing an object that does not respond to these selectors, thus an object that is not a Game object that you will get an exception, and that your app might break

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