我已经Workout
有Difficulty
属性
enum Difficulty: String {
case easy = "easy"
case moderate = "moderate"
case hard = "hard"
}
class Workout {
var name: String?
var difficulty: Difficulty?
.
.
.
}
我想通过难度属性对一系列训练进行排序。我知道我可以通过将枚举的原始值分配给Int值来实现这一点,并按如下方式比较这些值:
data.sort { $0.workout.difficulty!.rawValue < $1.workout.difficulty!.rawValue }
但是我真的希望这个枚举能够存储字符串,因为它可以方便地将它分配给标签文本而不会出现丑陋的开关案例,并且在某些方面具有可比性。
如何实现?
答案 0 :(得分:9)
在Comparable
上实施enum
协议。它为您提供了static func < (lhs: Difficulty, rhs: Difficulty) -> Bool
方法,您可以在其中定义排序。
以下是使用属性简化排序的完整示例
enum Difficulty: String, Comparable {
case easy = "easy"
case moderate = "moderate"
case hard(String) = "hard"
private var sortOrder: Int {
switch self {
case .easy:
return 0
case .moderate:
return 1
case .hard(_):
return 2
}
}
static func ==(lhs: Difficulty, rhs: Difficulty) -> Bool {
return lhs.sortOrder == rhs.sortOrder
}
static func <(lhs: Difficulty, rhs: Difficulty) -> Bool {
return lhs.sortOrder < rhs.sortOrder
}
}
可以使用
data.sort { $0.workout.difficulty! < $1.workout.difficulty! }
答案 1 :(得分:3)
您可以使用枚举hashValue对锻炼进行排序。您应该使用结构而不是类,并且类似于Igor建议的结构,您可以使结构具有可比性,而不是枚举:
struct Workout: Comparable {
let name: String
let difficulty: Difficulty
enum Difficulty: String {
case easy, moderate, hard
}
static func ==(lhs: Workout, rhs: Workout) -> Bool {
return lhs.difficulty.hashValue == rhs.difficulty.hashValue
}
static func <(lhs: Workout, rhs: Workout) -> Bool {
return lhs.difficulty.hashValue < rhs.difficulty.hashValue
}
}
let wk1 = Workout(name: "night", difficulty: .hard)
let wk2 = Workout(name: "morning", difficulty: .easy)
let wk3 = Workout(name: "afternoon", difficulty: .moderate)
let workouts = [wk1, wk2, wk3] // [{name "night", hard}, {name "morning", easy}, {name "afternoon", moderate}]
let sorted = workouts.sorted() // [{name "morning", easy}, {name "afternoon", moderate}, {name "night", hard}]