Swift:按属性对列表对象进行排序

时间:2018-08-06 15:04:56

标签: swift sorting

在网络上进行了大量研究后,我尝试了几种解决方案,但这行不通吗?我想根据LeagueCode属性对数组排序,这是我的代码:

var teams = [Team]()

    dbReference.child(FBDatabaseKeys.teams).observeSingleEvent(of: .value) { (snapshot) in
        for nameTeam in snapshot.children.allObjects as! [DataSnapshot] {
            let value = nameTeam.value as? NSDictionary

            let team = Team(
                capitanId: value?[FBDatabaseKeys.Teams.captainID] as! String,
                leagueCode: value?[FBDatabaseKeys.Teams.leagueCode] as? String ?? value?["leageCode"] as! String,
                playersIds: [""], //todo retrieve data to array
                teamName: value?[FBDatabaseKeys.Teams.teamName] as! String,
                imageUrl: value?[FBDatabaseKeys.Teams.imageUrl] as! String,
                groupMoto: value?[FBDatabaseKeys.Teams.teamMoto] as! String,
                totalScore: String(format: "%@", value?[FBDatabaseKeys.Teams.totalScore] as? CVarArg ?? "") ,
                totalTickets: String(format: "%@", value?[FBDatabaseKeys.Teams.totalTickets] as? CVarArg ?? "")
            )

            if team.leagueCode == self.currentPlayer.leagueCode{
                teams.append(team)
            }
        }

        teams = teams.sort {
            Int($0.leagueCode)! < Int($1.leagueCode)!
        } as! [Team]

        //Sort array by property score
        completion(teams)
    }

错误

  

错误:(204,27)无法将类型'()'的值分配给类型'[Team]'

这是我的对象:

struct Team {
let capitanId: String
let leagueCode: String
let playersIds: [String]
let teamName: String
let imageUrl: String
let groupMoto: String
let totalScore: String
let totalTickets: String

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您需要

teams  = teams.sorted {
  Int($0.leagueCode)! < Int($1.leagueCode)!
}

或仅mutating

teams.sort {
    Int($0.leagueCode)! < Int($1.leagueCode)!
}

https://developer.apple.com/documentation/swift/array/2296815-sorted https://developer.apple.com/documentation/swift/array/2296801-sort

//

dbReference.child(FBDatabaseKeys.teams).observeSingleEvent(of: .value) { (snapshot) in
        for nameTeam in snapshot.children.allObjects as! [DataSnapshot] {
            let value = nameTeam.value as? NSDictionary

            let team = Team(
                capitanId: value?[FBDatabaseKeys.Teams.captainID] as! String,
                leagueCode: value?[FBDatabaseKeys.Teams.leagueCode] as? String ?? value?["leageCode"] as! String,
                playersIds: [""], //todo retrieve data to array
                teamName: value?[FBDatabaseKeys.Teams.teamName] as! String,
                imageUrl: value?[FBDatabaseKeys.Teams.imageUrl] as! String,
                groupMoto: value?[FBDatabaseKeys.Teams.teamMoto] as! String,
                totalScore: String(format: "%@", value?[FBDatabaseKeys.Teams.totalScore] as? CVarArg ?? "") ,
                totalTickets: String(format: "%@", value?[FBDatabaseKeys.Teams.totalTickets] as? CVarArg ?? "")
            )

            if team.leagueCode == self.currentPlayer.leagueCode{
                teams.append(team)
            }
        }

        teams.sort {
            Int($0.leagueCode)! < Int($1.leagueCode)!
        }  


        completion(teams)
}

答案 1 :(得分:0)

错误消息

  

无法分配'()'类型的值

清楚地说sort函数不返回任何内容。数组按位置排序。

并且不需要转换为Int。您可以使用localizedStandardCompare

对字符串进行数字排序
teams.sort {
    $0.leagueCode.localizedStandardCompare($1.leagueCode) == .orderedAscending
}