简化一下模型,一般来说服务器返回的都是按时间顺序排好的数组(unsorted),排序后如result所示。
var result: Array<Any> = []
/// 里面代表月份
let unsorted = [9, 9, 9, 8, 8, 7, 6, 6, 5, 4, 4, 1]
billSort(unsorted)
/// 输出结果
result = [[9, 9, 9], [8, 8], [7], [6, 6], [5], [4, 4], [1]]
关键代码:
var result: Array<Any> = []
/// 用于保存上一次排序的月份,以便接下来比较
var olderValue: Int?
func billSort(_ a: [Int]) {
guard a.count > 1 else {
result.append(a)
return
}
olderValue = a.first
/// 取出同月份的数据
let currentMonth = a.filter{ $0 == olderValue }
result.append(currentMonth)
/// 取出不同月份的数据进行下一次比较
let rest = a.filter{ $0 != olderValue }
/// 判断是否还有未排序
if rest.isEmpty == false {
olderValue = rest.first
billSort(rest)
}
}
let unsorted = [9, 9, 9, 8, 8, 7, 6, 6, 5, 4, 4, 1]
billSort(unsorted)
print(result)
// result = [[9, 9, 9], [8, 8], [7], [6, 6], [5], [4, 4], [1]]
实际情况只需要将判定的条件换为是否同月。完整代码如下:
class BillModel {
var timeStamp: Double?
var money: Double?
init(_ time: Double, money: Double) {
self.timeStamp = time
self.money = money
}
}
func creatBill() -> [BillModel] {
var currentTime = NSDate().timeIntervalSince1970
var bills: [BillModel] = []
for x in 0 ..< 10 {
let bill = BillModel.init(currentTime, money: Double(x))
bills.append(bill)
currentTime -= 1000000
}
return bills
}
func isTheSameMonth(_ a: Double, b: Double) -> Bool {
let aDate = Date.init(timeIntervalSince1970: a)
let bDate = Date.init(timeIntervalSince1970: b)
let aM = Calendar.current.dateComponents([.month], from: aDate)
let bM = Calendar.current.dateComponents([.month], from: bDate)
return aM.month == bM.month && aM.year == bM.year
}
var result: Array<Any> = []
var olderValue: BillModel?
func billSort(_ a: [BillModel]) {
guard a.count > 1 else {
result.append(a)
return
}
olderValue = a.first
let currentMonth = a.filter{ isTheSameMonth($0.timeStamp!, b: (olderValue?.timeStamp)!) }
print(currentMonth)
result.append(currentMonth)
let rest = a.filter{ !isTheSameMonth($0.timeStamp!, b: (olderValue?.timeStamp)!) }
if rest.isEmpty == false {
olderValue = rest.first
billSort(rest)
}
}
let bills = creatBill()
billSort(bills)
print(result)