简体   繁体   中英

Sort an array of object using string parameter in Swift3 iOS

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.

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 {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM