这是一种可能的解决方案,它使用Dictionary
s 内置排序功能,但是title
在您的示例中将属性显示为自定义enum
而不是String
. 然后,“排序模板”由enum
.
即,enum MonthSortTemplate
你的班级MyClass
(后者你没有给我们洗澡,所以我自己做了一个 MWE)为:
enum MonthSortTemplate: Int {
case April = 1
case January
case February
// ... rest of months follows, in the order you prefer
}
class MyClass {
var price = ""
var value = ""
var title: MonthSortTemplate = .April
}
// Hashable (just some dummy, don't know how you've set this up)
extension MyClass: Hashable {
var hashValue: Int {
return price.hashValue ^ value.hashValue
}
}
// Equatable (just some dummy, don't know how you've set this up)
func ==(lhs: MyClass, rhs: MyClass) -> Bool {
return lhs.price == rhs.price && lhs.value == rhs.value
}
创建您的MyClass
实例,添加到您的字典中,并将.sort(...)
后者的函数用于自定义闭包,为这种特定类型的比较指定。
var Obj1 = MyClass()
var Obj2 = MyClass()
var Obj3 = MyClass()
Obj1.price = "10"
Obj1.value = "abc"
Obj1.title = .January
Obj2.price = "10"
Obj2.value = "def"
Obj2.title = .April
Obj3.price = "10"
Obj3.value = "pqr"
Obj3.title = .February
var dict = Dictionary<MyClass, Array<Int32>>()
dict = [ Obj1: [3,4], Obj2 : [1,2], Obj3: [8,9]]
// your custom sort closure, for Dictionary.sort(...) method
let byMonthTemplate = {
(elem1:(key: MyClass, val: [Int32]), elem2:(key: MyClass, val: [Int32]))->Bool in
if elem1.key.title.rawValue < elem2.key.title.rawValue {
return true
} else {
return false
}
}
let sortedDict = dict.sort(byMonthTemplate)
print("\(dict)")
另一种选择——如果你真的喜欢你的类属性title
是类型String
——是为对象定义<
运算符MyClass
:
func <(lhs: MyClass, rhs: MyClass) -> Bool {
// do comparison stuff with strings lhs.title and rhs.title
// with regard to your ordering of choice (array sortTemplate)
return ...
}
在这种情况下,“凌乱”的东西最终会出现在这个函数中,而实际的排序可以非常优雅地执行
let sortedDict = dict.sort { $0.0 < $1.0 }
就个人而言,我更喜欢这个enum
解决方案(但是,这是题外话)。
编辑:
根据您的要求,我将为<
您的班级提供一个运算符示例MyClass
。这绝不是最佳的,但也许你可以从我的例子中改进它。
// add sortTemplate as a static property of MyClass
class MyClass {
var price = ""
var value = ""
var title = ""
static let sortTemplate = ["April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February", "March"]
}
// define < operator for MyClass objects
func <(lhs: MyClass, rhs: MyClass) -> Bool {
let indexOfLhs = MyClass.sortTemplate.indexOf({$0 == lhs.title})
let indexOfRhs = MyClass.sortTemplate.indexOf({$0 == rhs.title})
return indexOfLhs < indexOfRhs
}
// you can now sort your dictionary according to
let sortedDict = dict.sort { $0.0 < $1.0 }