我之前已经问过这个问题但尚未找到有效的解决方案。 我想要的是获得一个年龄范围内的用户列表,并按照“创建过程”排序。时间戳,但由于生日'范围过滤第一个订单必须在生日'字段。
这是我的疑问:
database.collection("users")
.whereField("birthday", isGreaterThan: exploreSettings.upperAgeDate)
.whereField("birthday", isLessThan: exploreSettings.lowerAgeDate)
.whereField("isHidden", isEqualTo: false)
.order(by: "birthday", descending: true)
.order(by: "createdAt", descending: true)
.limit(to: 8)
.start(afterDocument: lastSnapshot)
.getDocuments {...}
这就是它将返回的内容
[
["name": "John", "age": 21, "createdAt": 2005],
["name": "Jane", "age": 22, "createdAt": 2003],
["name": "Adam", "age": 23, "createdAt": 2004],
["name": "Karl", "age": 24, "createdAt": 2001],
["name": "Mila", "age": 25, "createdAt": 2002],
]
我希望它返回
[
["name": "Karl", "age": 24, "createdAt": 2001],
["name": "Mila", "age": 25, "createdAt": 2002],
["name": "Jane", "age": 22, "createdAt": 2003],
["name": "Adam", "age": 23, "createdAt": 2004],
["name": "John", "age": 21, "createdAt": 2005],
]
有些人建议我以某种方式将生日和时间戳合并到一个字段中,但我知道我应该如何做到这一点
答案 0 :(得分:0)
尝试使用此功能:
let arrResponse = [
["name": "John", "age": 21, "createdAt": 2005],
["name": "Jane", "age": 22, "createdAt": 2003],
["name": "Adam", "age": 23, "createdAt": 2004],
["name": "Karl", "age": 24, "createdAt": 2001],
["name": "Mila", "age": 25, "createdAt": 2002],
]
let descriptor : NSSortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
let sortedResult = (arrResponse as NSArray).sortedArray(using: [descriptor])
print(sortedResult)
输出是这样的: