根据优先级对 NSMutableArray 进行排序
Sorting NSMutableArray depending on priority
我有一个字典的 NSMutableArray。
dic1 = ["user":"Smith","points":12,"lastPoints":4,online:3MonthsAgo(NSDate)]
dic2 = ["user":"Mike","points":22,"lastPoints":3,online:2MonthsAgo(NSDate)]
dic3 = ["user":"Mark","points":22,"lastRank":5,online:now(NSDate)]
dic4 = ["user":"Jay","points":16,"lastPoints":2,online:2minutes(NSDate)]
dic5 = ["user":"Alex","points":13,"lastPoints":5,online:1hr(NSDate)]
dic6 = ["user":"Peter","points":22,"lastPoints":3,online:2days(NSDate)]
我想通过 3 种方式重新安排,
积分最高的第一批用户将最先显示
所以
Mike:22
Mark:22
Peter:22
Jay:16
Alex:13
Smith:12
现在我确实使用了
let sort = NSSortDescriptor(key: "points", ascending: false)
接下来我要根据 lastPoints 示例
再次对这些用户进行排序
Mark:22:5
Mike:22:3
Peter:22:3
Jay:16:2
Alex:13:5
Smith:12:4
然后根据上次在线日期对 Mike 和 Peter 在数组中的位置进行排序。
现在当我尝试
let sortAgain = NSSortDescriptor(key: "lastPoints", ascending: false) and then
let sortAgain2 = NSSortDescriptor(key: "online", ascending: false)
它删除了我之前所做的 points 排序,并根据 lastPoints 再次对所有内容进行排序。
我希望它根据点对它们进行排序,然后查看点是否相等,根据 lastPoints 对这些点数据进行排序。如果 lastPoints 相等,则根据 "Online" 日期对这些相等数据进行排序。
我建议使用 sort
(或 sortInPlace
)函数,您可以在其中指定自定义排序块。例如,如果您将所有 Dictionaries
放入 Array
,您可以执行以下操作:
array.sort { (dict1, dict2) -> Bool in
if let p1 = dictionary1["points"] as? Int, let p2 = dictionary2["points"] as? Int {
if let l1 = dictionary1["lastPoints"] as? Int, let l2 = dictionary2["lastPoints"] as? Int where p1 == p2 {
return l1 > l2
}
return p1 > p2
}
return false
}
您必须自己进行合适的实施,但这个概念应该适合您。
试试这个:
let result = arr.sort {
let p0 = [=10=]["points"] as! Int
let p1 = ["points"] as! Int
let lp0 = [=10=]["lastPoints"] as! Int
let lp1 = ["lastPoints"] as! Int
let online0 = [=10=]["online"] as! NSDate
let online1 = ["online"] as! NSDate
if p0 != p1 {
return p0 > p1
} else if lp0 != lp1 {
return lp0 > lp1
} else {
return online0.timeIntervalSince1970 > online1.timeIntervalSince1970
}
}
我有一个字典的 NSMutableArray。
dic1 = ["user":"Smith","points":12,"lastPoints":4,online:3MonthsAgo(NSDate)]
dic2 = ["user":"Mike","points":22,"lastPoints":3,online:2MonthsAgo(NSDate)]
dic3 = ["user":"Mark","points":22,"lastRank":5,online:now(NSDate)]
dic4 = ["user":"Jay","points":16,"lastPoints":2,online:2minutes(NSDate)]
dic5 = ["user":"Alex","points":13,"lastPoints":5,online:1hr(NSDate)]
dic6 = ["user":"Peter","points":22,"lastPoints":3,online:2days(NSDate)]
我想通过 3 种方式重新安排, 积分最高的第一批用户将最先显示 所以
Mike:22
Mark:22
Peter:22
Jay:16
Alex:13
Smith:12
现在我确实使用了
let sort = NSSortDescriptor(key: "points", ascending: false)
接下来我要根据 lastPoints 示例
再次对这些用户进行排序Mark:22:5
Mike:22:3
Peter:22:3
Jay:16:2
Alex:13:5
Smith:12:4
然后根据上次在线日期对 Mike 和 Peter 在数组中的位置进行排序。 现在当我尝试
let sortAgain = NSSortDescriptor(key: "lastPoints", ascending: false) and then
let sortAgain2 = NSSortDescriptor(key: "online", ascending: false)
它删除了我之前所做的 points 排序,并根据 lastPoints 再次对所有内容进行排序。 我希望它根据点对它们进行排序,然后查看点是否相等,根据 lastPoints 对这些点数据进行排序。如果 lastPoints 相等,则根据 "Online" 日期对这些相等数据进行排序。
我建议使用 sort
(或 sortInPlace
)函数,您可以在其中指定自定义排序块。例如,如果您将所有 Dictionaries
放入 Array
,您可以执行以下操作:
array.sort { (dict1, dict2) -> Bool in
if let p1 = dictionary1["points"] as? Int, let p2 = dictionary2["points"] as? Int {
if let l1 = dictionary1["lastPoints"] as? Int, let l2 = dictionary2["lastPoints"] as? Int where p1 == p2 {
return l1 > l2
}
return p1 > p2
}
return false
}
您必须自己进行合适的实施,但这个概念应该适合您。
试试这个:
let result = arr.sort {
let p0 = [=10=]["points"] as! Int
let p1 = ["points"] as! Int
let lp0 = [=10=]["lastPoints"] as! Int
let lp1 = ["lastPoints"] as! Int
let online0 = [=10=]["online"] as! NSDate
let online1 = ["online"] as! NSDate
if p0 != p1 {
return p0 > p1
} else if lp0 != lp1 {
return lp0 > lp1
} else {
return online0.timeIntervalSince1970 > online1.timeIntervalSince1970
}
}