我正在尝试通过“dateRanges”对这个数组进行排序,这是一个NSDate对象。
- [0]
- name : "BREAKFAST"
- dateRanges
- [0]
- [0] : 2015-06-18 08:00:00 +0000
- [1]
- [0] : 2015-06-19 19:00:00 +0000
- hours
- [0] : 2
- [1] : 2
- [1]
- name : "LUNCH"
- dateRanges
- [0]
- [0] : 2015-06-16 08:00:00 +0000
- hours
- [0] : 8
- [2]
- name : "DINNER"
- dateRanges
- [0]
- [0] : 2015-06-17 06:30:00 +0000
- [1]
- [0] : 2015-06-30 16:00:00 +0000
- [2]
- [0] : 2015-06-12 17:00:00 +0000
- [3]
- [0] : 2015-06-29 09:30:00 +0000
- hours
- [0] : 1
- [1] : 2
- [2] : 3
- [3] : 3
我的工作是致电:
extension NSDate: Comparable { }
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
}
result.sortInPlace({ $0.dateRanges[0][0].compare($1.dateRanges[0][0]) == NSComparisonResult.OrderedAscending })
但输出结果如下:
- [0]
- name : "LUNCH"
- dateRanges
- [0]
- [0] : 2015-06-16 08:00:00 +0000
- hours
- [0] : 8
- [1]
- name : "DINNER"
- dateRanges
- [0]
- [0] : 2015-06-17 06:30:00 +0000
- [1]
- [0] : 2015-06-30 16:00:00 +0000
- [2]
- [0] : 2015-06-12 17:00:00 +0000
- [3]
- [0] : 2015-06-29 09:30:00 +0000
- hours
- [0] : 1
- [1] : 2
- [2] : 3
- [3] : 3
- [2]
- name : "BREAKFAST"
- dateRanges
- [0]
- [0] : 2015-06-18 08:00:00 +0000
- [1]
- [0] : 2015-06-19 19:00:00 +0000
- hours
- [0] : 2
- [1] : 2
正如你所看到的,它只按dateRanges [0] [0]索引排序,(显然是因为我在sortInPlace方法中问它),但我想知道如何通过dateRanges对这个数组进行排序的索引。 预期结果应为:
- [0]
- name : "LUNCH"
- dateRanges
- [0]
- [0] : 2015-06-16 08:00:00 +0000
- hours
- [0] : 8
- [1]
- name : "DINNER"
- dateRanges
- [0]
- [0] : 2015-06-12 17:00:00 +0000
- [1]
- [0] : 2015-06-17 06:30:00 +0000
- [2]
- [0] : 2015-06-29 09:30:00 +0000
- [3]
- [0] : 2015-06-30 16:00:00 +0000
- hours
- [0] : 1
- [1] : 2
- [2] : 3
- [3] : 3
- [2]
- name : "BREAKFAST"
- dateRanges
- [0]
- [0] : 2015-06-18 08:00:00 +0000
- [1]
- [0] : 2015-06-19 19:00:00 +0000
- hours
- [0] : 2
- [1] : 2
答案 0 :(得分:0)
当我仔细观察你想要的结果时,似乎它没有以任何方式排序,你仍然按0索引排序。所以如果这真的是你的目标,那么你需要做的就是:
MyArray.sortByZeroIndices() //here you use your code
for item in MyArray {
item.dateRanges.sort(<) // sort every internal array
}