如何按对象的多个属性对自定义对象列表进行排序
How can I sort list of custom object by multiple properties of objects
谁能帮我按多个参数对自定义对象列表进行排序。
我想按 date1 和 date2 对它进行排序,并可选择 b 所有三个属性,但无论我尝试什么都失败了:(
我的对象:
class MyObject : NSManagedObject{
@NSManaged var date1:NSDate
@NSManaged var date2:NSDate
@NSManaged var key:Int
}
这是我对这些对象列表进行排序的失败尝试:
var sortedArray1 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in{
return o1.date1 == o2.date1 ? (o1.date2 < o2.date2) : (o1.date1 < o2.date1)
}
var sortedArray2 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in
return (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) == (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) ? (o1.date2.compare(o2.date2) == NSComparisonResult.OrderedAscending) : (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending))
});
试试这个并告诉我:
unorderedList.sorted({
if [=10=].date1 < .date1 {
return true
} else if [=10=].date1 == $date2 {
return [=10=].date2 < .date2
}
return false
})
public func == (left: NSDate, right: NSDate) -> Bool {
return left.isEqualToDate(right)
}
public func != (left: NSDate, right: NSDate) -> Bool {
return !left.isEqualToDate(right)
}
public func > (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == NSComparisonResult.OrderedDescending
}
public func < (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == NSComparisonResult.OrderedAscending
}
谁能帮我按多个参数对自定义对象列表进行排序。
我想按 date1 和 date2 对它进行排序,并可选择 b 所有三个属性,但无论我尝试什么都失败了:(
我的对象:
class MyObject : NSManagedObject{
@NSManaged var date1:NSDate
@NSManaged var date2:NSDate
@NSManaged var key:Int
}
这是我对这些对象列表进行排序的失败尝试:
var sortedArray1 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in{
return o1.date1 == o2.date1 ? (o1.date2 < o2.date2) : (o1.date1 < o2.date1)
}
var sortedArray2 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in
return (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) == (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) ? (o1.date2.compare(o2.date2) == NSComparisonResult.OrderedAscending) : (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending))
});
试试这个并告诉我:
unorderedList.sorted({
if [=10=].date1 < .date1 {
return true
} else if [=10=].date1 == $date2 {
return [=10=].date2 < .date2
}
return false
})
public func == (left: NSDate, right: NSDate) -> Bool {
return left.isEqualToDate(right)
}
public func != (left: NSDate, right: NSDate) -> Bool {
return !left.isEqualToDate(right)
}
public func > (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == NSComparisonResult.OrderedDescending
}
public func < (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == NSComparisonResult.OrderedAscending
}