Reputation: 716
I have to sort an array of object using one parameter. I have created an entity, below is my JSON :
{
"Name": "Amit",
"Progress": "38",
"Color": "red",
},
{
"Name": "Sonu",
"Progress": "70",
"Color": "green",
}
Below is my entity class:
class Scoreboard {
// MARK: - Properties
var scoreboardName: String? = ""
var scoreboardProgress: String? = "0"
var scoreboardColor: String? = ""
}
This JSON data I have pass into table, The problem is How can I do sorting based on 'Progress'?
I tried below code:
scoreboardArray = scoreboardArray.sorted(by: {($0.scoreboardProgress() < $1.scoreboardProgress())})
But it's not working. Please anyone suggest me.
Upvotes: 0
Views: 45
Reputation: 100503
Can you try
scoreboardArray = scoreboardArray.sorted {
Int($0.scoreboardProgress!)! < Int($1.scoreboardProgress!)!
}
also if you assign a default value , it's better to remove ?
Irrelevant but why not use Decodable
class Scoreboard : Decodable {
Upvotes: 2