swift - 将 nil 日期排序到数组的末尾

标签 swift sorting date option-type

尝试按降序对 Swift 中的数组进行排序。这很好用

objectArray.sort{ $0.date!.compare($1.date!) == .orderedDescending}

如您所见,我正在强制展开日期。我正在寻找另一种方法,以便在日期为 nil 时,对象移动到数组的末尾。

最佳答案

也许不是最干净的解决方案,但您可以使用 nil-coalescing 一步完成。

objectArray.sort{ ($0.date ?? .distantPast) > ($1.date ?? .distantPast) }

关于swift - 将 nil 日期排序到数组的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44144298/

相关文章:

swift - UICollectionViewCells中selectedBackGroundView是显示在contentView上方还是下方?

ios - 当应用程序在后台时检索当前位置

ios - Swift 3 登录函数

ios - 为collectionView添加deleteItemAtIndexPath

bash 。关联数组迭代(有序且无重复)

java - 将 JSpinner 的日期值转换为整数

algorithm - 计算数组中的值 "surround"数组外的不同值

Python 3 : How can I check if two lists match even if elements are not in order

javascript - 比较 Javascript 日期与 php 当前日期

r - 将字符转换为日期