已排序 Array/Map Swift
Sorted Array/Map Swift
我执行了一个 GET 请求,然后我将数据保存在 data 中,像这样
if let data: [MyMap] = response?.results {
let var_to_sort = data
self.generateStuff(var: var_to_sort)
}
MyMap 是这样定义的可映射对象:
class MyMap: NSCoding {
var name: String?
var score: Score?
...
使用映射、编码等...
分数同上:
class Score:NSCoding {
var number: Double?
...
我需要的是根据数量(从大到小)对var_to_sort = data进行排序并传递到 generateStuff 方法。请记住 response.results 有不止 1 个对象。有人可以帮助我吗?
只需对数组进行排序:
let var_to_sort = data.sorted {
[=10=].score?.number ?? 0 > .score?.number ?? 0
}
我确实有适合您需要的可行解决方案。并且可能是一种通过在不需要时尝试避免可选项来改进代码的方法。如果您的网络服务总是 return 对象的名称和分数,那么请不要使用可选值,这会让您的生活更轻松。您可以在我的解决方案中看到,当我没有任何可选代码时,代码更易于阅读,而且这样的操作也很简单。
// -------------- Solution 1 avoiding optionals ----------------
struct MyObject {
var name: String
var score: Score
}
struct Score {
var number: Double
}
let data: [MyObject] = [MyObject(name: "obj1", score: Score(number: 12)),
MyObject(name: "obj2", score: Score(number: 2)),
MyObject(name: "obj3", score: Score(number: 24)),
MyObject(name: "obj4", score: Score(number: 4)),
MyObject(name: "obj5", score: Score(number: 1))]
let sortedObj = data.sorted(by: { [=10=].score.number > .score.number })
print(sortedObj)
// Print: obj3, obj1, obj4, obj2 and obj5
// -------------- Solution 2 using optionals ----------------
struct MyObject2 {
var name: String?
var score: Score2?
}
struct Score2 {
var number: Double?
}
let data2: [MyObject2] = [MyObject2(name: "obj1", score: Score2(number: 12)),
MyObject2(name: "obj2", score: Score2(number: 2)),
MyObject2(name: "obj3", score: Score2(number: 24)),
MyObject2(name: "obj4", score: Score2(number: 4)),
MyObject2(name: "obj5", score: Score2(number: 1))]
let sortedObj2 = data2.sorted(by: {
guard let num1 = [=10=].score?.number, let num2 = .score?.number else {
return false
}
return num1 > num2
})
print(sortedObj2)
// Print: obj3, obj1, obj4, obj2 and obj5
希望对您有所帮助
按此方式对数组进行排序。
data2.sort(by:{[=10=].score?.number > .score?.number})
我执行了一个 GET 请求,然后我将数据保存在 data 中,像这样
if let data: [MyMap] = response?.results {
let var_to_sort = data
self.generateStuff(var: var_to_sort)
}
MyMap 是这样定义的可映射对象:
class MyMap: NSCoding {
var name: String?
var score: Score?
...
使用映射、编码等... 分数同上:
class Score:NSCoding {
var number: Double?
...
我需要的是根据数量(从大到小)对var_to_sort = data进行排序并传递到 generateStuff 方法。请记住 response.results 有不止 1 个对象。有人可以帮助我吗?
只需对数组进行排序:
let var_to_sort = data.sorted {
[=10=].score?.number ?? 0 > .score?.number ?? 0
}
我确实有适合您需要的可行解决方案。并且可能是一种通过在不需要时尝试避免可选项来改进代码的方法。如果您的网络服务总是 return 对象的名称和分数,那么请不要使用可选值,这会让您的生活更轻松。您可以在我的解决方案中看到,当我没有任何可选代码时,代码更易于阅读,而且这样的操作也很简单。
// -------------- Solution 1 avoiding optionals ----------------
struct MyObject {
var name: String
var score: Score
}
struct Score {
var number: Double
}
let data: [MyObject] = [MyObject(name: "obj1", score: Score(number: 12)),
MyObject(name: "obj2", score: Score(number: 2)),
MyObject(name: "obj3", score: Score(number: 24)),
MyObject(name: "obj4", score: Score(number: 4)),
MyObject(name: "obj5", score: Score(number: 1))]
let sortedObj = data.sorted(by: { [=10=].score.number > .score.number })
print(sortedObj)
// Print: obj3, obj1, obj4, obj2 and obj5
// -------------- Solution 2 using optionals ----------------
struct MyObject2 {
var name: String?
var score: Score2?
}
struct Score2 {
var number: Double?
}
let data2: [MyObject2] = [MyObject2(name: "obj1", score: Score2(number: 12)),
MyObject2(name: "obj2", score: Score2(number: 2)),
MyObject2(name: "obj3", score: Score2(number: 24)),
MyObject2(name: "obj4", score: Score2(number: 4)),
MyObject2(name: "obj5", score: Score2(number: 1))]
let sortedObj2 = data2.sorted(by: {
guard let num1 = [=10=].score?.number, let num2 = .score?.number else {
return false
}
return num1 > num2
})
print(sortedObj2)
// Print: obj3, obj1, obj4, obj2 and obj5
希望对您有所帮助
按此方式对数组进行排序。
data2.sort(by:{[=10=].score?.number > .score?.number})