ios 如何按降序对日期数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14541458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort an array of dates in descending order
提问by baste
I have a NSDictionary that parsed to an array one of the element is date, i tried using [startimeArray sortUsingSelector:@selector(compare:)];
(starttimeArray) is my date but it only arrange ascending. how can i sort in descending order. thanks
我有一个 NSDictionary 解析为一个数组,其中一个元素是日期,我尝试使用[startimeArray sortUsingSelector:@selector(compare:)];
(starttimeArray) 是我的日期,但它只按升序排列。我如何按降序排序。谢谢
回答by Anoop Vaidya
Sort the array by puting NOis ascending parameter:
通过放置NO是升序参数对数组进行排序:
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors];
回答by vikingosegundo
You can use a comparator block
您可以使用比较器块
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(NSDate *d1, NSDate *d2) {
return [d1 compare:d2];
}];
to reverse the order, just swap the dates
要颠倒顺序,只需交换日期
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(NSDate *d1, NSDate *d2) {
return [d2 compare:d1];
}];
or — as compare:
returns a NSComparisonResult, which is actually typedef'ed to integer see below— just multiply by -1
或者——作为compare:
返回一个 NSComparisonResult,它实际上被 typedef'ed 为整数,见下文——只需乘以 -1
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(NSDate *d1, NSDate *d2) {
return -1* [d1 compare:d2];
}];
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
回答by sarbuLopex
I'd use the built-in reverseObjectEnumerator
method of NSArray
我会使用内置的reverseObjectEnumerator
方法NSArray
startimeArray = [startimeArray sortUsingSelector:@selector(compare:)];
startimeArray = [[startimeArray reverseObjectEnumerator] allObjects];
It's useful when you need the array sorted in ascending and descending order, so you can easily go back to the previous sort.
当您需要按升序和降序对数组进行排序时,它很有用,因此您可以轻松地返回到之前的排序。
回答by Kevin ABRIOUX
There is the swift solution for the checked solution :
检查的解决方案有一个快速的解决方案:
let sortedArray = randomArray.sortedArrayUsingComparator { (d1, d2) -> NSComparisonResult in
return (d1 as! NSDate).compare(d2 as! NSDate)
}
回答by Daniel
Swift 3.0
斯威夫特 3.0
For anyone using swift 3.0 this DateHelperwill save u some headache.
对于任何使用 swift 3.0 的人来说,这个DateHelper会为你省去一些麻烦。
filteredLogs.sort(by: {
let firstElement = -(NSComparisonResult) compareArray: (id) element {
Return [ArrayElement compare: [element ArrayElement]];
}
-(NSComparisonResult) compareArrayReverse: (id) element {
Return [[element ArrayElement] compare: ArrayElement];
}
.timeStamp! as Date
let secondElement = .timeStamp! as Date
return firstElement.compare(.isEarlier(than: secondElement))
})
The above code is a snippet from one of my projects. It uses the helper class to sort my logs with most recent dates at the left of the array. The $ sign is a positional parameter
上面的代码是我的一个项目的片段。它使用 helper 类将我的日志与数组左侧的最新日期进行排序。$ 符号是一个位置参数
回答by jacai
In the Class where the Array elements are being initialized.
在初始化数组元素的类中。
@synthesize ArrayName;
-(void) sort: (BOOL) sel {
if (sel) {
[ArrayName sortUsingSelector: @selector(compareArray:)];
} else {
[ArrayName sortUsingSelector: @selector(compareArrayReverse:)];
}
}
Then in the Class where your startimeArray is initialized:
然后在初始化 starttimeArray 的类中:
// Sort the array. sort = TRUE - ascending order, sort = FALSE - descending order.
[startimeArray sort: FALSE];
Then in the main():
然后在 main() 中:
##代码##