对 NSDate 的 NSArray 进行排序

发布于 2024-10-06 05:01:19 字数 90 浏览 0 评论 0原文

我有一个 NSDate 对象的 NSArray,我想对它们进行排序,以便今天在 0,昨天在 1 等。

它是升序还是降序,我是否使用函数、选择器或什么?

I have an NSArray of NSDate objects and I want to sort them so that today is at 0, yesterday at 1 etc.

Is it ascending or descending, and do i use a function, selector or what?

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

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

发布评论

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

评论(2

ま昔日黯然 2024-10-13 05:01:19

NSArray 有不同的排序方法,因为您可能需要不同的排序方式。 NSSortDescriptors 是一种通用方法,它为您提供了很多选项,包括在排序中使用哪些键、要在这些键上使用哪些选择器以及总体使用顺序等。或者您可以使用函数或比较器块来代替,如果您的情况需要这样做,或者这对您的具体情况是否更方便。

回答你的第一个问题,如果你希望今天是第一位,然后是昨天,那么,是的,这当然是降序。

要按降序对某些日期进行排序,您可以这样做:(假设一个充满 NSDates 的 NSArray 称为“dateArray”):

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
[descriptor release];

NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];

或者,如果您正在为 iOS 4+ 或 Snow Leopard+ 构建,您可以这样做:

NSArray *reverseOrderUsingComparator = [dateArray sortedArrayUsingComparator: 
                                       ^(id obj1, id obj2) {
                                           return [obj2 compare:obj1]; // note reversed comparison here
                                       }];

There are different sort methods for NSArray because there may be different ways you want to sort things. NSSortDescriptors are a general way that give you a lot of options as far as what keys to use in sorting, what selectors you want to use on those keys, and overall order to use, etc. Or you can use functions or comparator blocks instead if your case requires that or if that's more convenient for your particular case.

To answer your first question, if you want today to be first, followed by yesterday then, yes, that is of course descending order.

To sort some dates in descending order you can just do this: (assuming an NSArray full of NSDates called 'dateArray'):

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
[descriptor release];

NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];

Or, if you are building for iOS 4+ or Snow Leopard+ you can do this:

NSArray *reverseOrderUsingComparator = [dateArray sortedArrayUsingComparator: 
                                       ^(id obj1, id obj2) {
                                           return [obj2 compare:obj1]; // note reversed comparison here
                                       }];
暖阳 2024-10-13 05:01:19

尝试这个魔术:

按升序对日期数组进行排序。

即日期越来越晚,或者换句话说,日期即将到来。

NSArray ascendingDates = [dates sortedArrayUsingSelector:@selector(compare:)];

排序按降序排列的日期数组。(问题所问的内容)

即日期越来越早,日期进入过去,或者换句话说:今天在索引 0 处,昨天在索引 1 处。

NSArray* descendingDates = [[[dates sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];

Try this magic:

Sort array of dates in ascending order.

i.e. dates getting later and later, or to put it another way, dates going into the future.

NSArray ascendingDates = [dates sortedArrayUsingSelector:@selector(compare:)];

Sort array of dates in descending order. (what the question asked)

i.e. dates getting earlier and earlier, dates going into the past or to put it another way: today is at index 0, yesterday at index 1.

NSArray* descendingDates = [[[dates sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文