gpt4 book ai didi

ios - 使用不同参数快速对自定义对象数组进行排序

转载 作者:行者123 更新时间:2023-11-29 01:12:47 25 4
gpt4 key购买 nike

我有一个自定义类的数组

class GameInfo {
var gameStatus :String
var country : String
var isSelected : Bool
}

这里,gameStatus可能是"*"/"1-0"/"1/2-1/2"

我想先按isSelected 对数组进行排序,然后是gameStatus,然后是country

我尝试了 sortInPlace 但对我不起作用。

假设这是我的数组

let list = [
GameInfo(gameStatus: "*", country: "Italy", isSelected: true),
GameInfo(gameStatus: "1/2-1/2", country: "France", isSelected: true),
GameInfo(gameStatus: "1-0", country: "Italy", isSelected: false),
GameInfo(gameStatus: "*", country: "Germany", isSelected: false),
GameInfo(gameStatus: "0-1", country: "Italy", isSelected: true),
GameInfo(gameStatus: "1/2-1/2", country: "France", isSelected: false),
GameInfo(gameStatus: "*", country: "UK", isSelected: false)
]

如果在选定的游戏中有任何实时游戏,即使在与我的国家相关的任何游戏中,该游戏也应位于顶部,则选定的游戏应位于顶部

假设我的国家是意大利那么数组的排序顺序是

     GameInfo(gameStatus: "*", country: "Italy", isSelected: true),
GameInfo(gameStatus: "0-1", country: "Italy", isSelected: true),
GameInfo(gameStatus: "1/2-1/2", country: "France", isSelected: true),
GameInfo(gameStatus: "*", country: "Germany", isSelected: false),
GameInfo(gameStatus: "*", country: "UK", isSelected: false)
GameInfo(gameStatus: "1-0", country: "Italy", isSelected: false),
GameInfo(gameStatus: "1/2-1/2", country: "France", isSelected: false),

最佳答案

方案一

您可以将自己的逻辑写入传递给 sort 的闭包中。

所以给定这个类

class GameInfo {
var gameStatus: String
var country: String
var isSelected: Bool

init(gameStatus: String, country: String, isSelected: Bool) {
self.gameStatus = gameStatus
self.country = country
self.isSelected = isSelected
}
}

和这个列表

let list = [
GameInfo(gameStatus: "A", country: "Italy", isSelected: true),
GameInfo(gameStatus: "A", country: "France", isSelected: true),
GameInfo(gameStatus: "B", country: "UK", isSelected: true),
GameInfo(gameStatus: "B", country: "Germany", isSelected: false),
GameInfo(gameStatus: "A", country: "Italy", isSelected: true),
GameInfo(gameStatus: "B", country: "UK", isSelected: false)
]

你可以这样创建一个排序列表

let sorted = list.sort {
guard $0.isSelected == $1.isSelected else { return $0.isSelected }
guard $0.gameStatus == $1.gameStatus else { return $0.gameStatus < $1.gameStatus }
guard $0.country == $1.country else { return $0.country < $1.country }
return true
}

解决方案2

您可以使您的GameInfo 类符合Comparable

class GameInfo: Comparable {
var gameStatus: String
var country: String
var isSelected: Bool

init(gameStatus: String, country: String, isSelected: Bool) {
self.gameStatus = gameStatus
self.country = country
self.isSelected = isSelected
}
}

func ==(left: GameInfo, right: GameInfo) -> Bool {
return
left.isSelected == right.isSelected &&
left.gameStatus == right.gameStatus &&
left.country == right.country
}

func <(left: GameInfo, right: GameInfo) -> Bool {
guard left.isSelected == right.isSelected else { return left.isSelected }
guard left.gameStatus == right.gameStatus else { return left.gameStatus < right.gameStatus }
guard left.country == right.country else { return left.country < right.country }
return true
}

现在您可以调用 sort 而无需进一步的参数。

let sorted = list.sort()

关于ios - 使用不同参数快速对自定义对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35554952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com