在 Swift3 中使用字符串参数对对象数组进行排序 iOS
Sort an array of object using string parameter in Swift3 iOS
我必须使用一个参数对一组对象进行排序。我创建了一个实体,下面是我的 JSON :
{
"Name": "Amit",
"Progress": "38",
"Color": "red",
},
{
"Name": "Sonu",
"Progress": "70",
"Color": "green",
}
下面是我的实体class:
class Scoreboard {
// MARK: - Properties
var scoreboardName: String? = ""
var scoreboardProgress: String? = "0"
var scoreboardColor: String? = ""
}
这个JSON数据我传给了table,问题是如何根据'Progress'进行排序?
我尝试了以下代码:
scoreboardArray = scoreboardArray.sorted(by: {([=13=].scoreboardProgress() < .scoreboardProgress())})
但它不起作用。请任何人推荐我。
你能试试吗
scoreboardArray = scoreboardArray.sorted {
Int([=10=].scoreboardProgress!)! < Int(.scoreboardProgress!)!
}
此外,如果您分配默认值,最好删除 ?
不相关,但为什么不使用 Decodable
class Scoreboard : Decodable {
我必须使用一个参数对一组对象进行排序。我创建了一个实体,下面是我的 JSON :
{
"Name": "Amit",
"Progress": "38",
"Color": "red",
},
{
"Name": "Sonu",
"Progress": "70",
"Color": "green",
}
下面是我的实体class:
class Scoreboard {
// MARK: - Properties
var scoreboardName: String? = ""
var scoreboardProgress: String? = "0"
var scoreboardColor: String? = ""
}
这个JSON数据我传给了table,问题是如何根据'Progress'进行排序?
我尝试了以下代码:
scoreboardArray = scoreboardArray.sorted(by: {([=13=].scoreboardProgress() < .scoreboardProgress())})
但它不起作用。请任何人推荐我。
你能试试吗
scoreboardArray = scoreboardArray.sorted {
Int([=10=].scoreboardProgress!)! < Int(.scoreboardProgress!)!
}
此外,如果您分配默认值,最好删除 ?
不相关,但为什么不使用 Decodable
class Scoreboard : Decodable {