Sorting lists alphabetically

Hi @hollance

I am having a little trouble sorting the checklist items. Although my app isn’t checklists (it’s teams and players) it has the same data model each Team has an array of Players

How would you do this? I am either getting compilation errors around no member types or when I run the app the new player I’m adding to a team isn’t added. What is the approach I should be taking?

What have you tried so far?

Well I went for this:

I added a sortPlayers() method into my Team Model.

    // order team alphabetically
func sortPlayers() {
    teamPlayers.sortInPlace({ player1, player2 in return
        player1.firstName.localizedStandardCompare(player2.firstName) == .OrderedAscending})
}

Within my Team Model I have var teamPlayers = [Player]() to store players of each team. In my PlayerViewController within the delegated didFinishEditing I have:

    // implement delegate method didFinishEditing
func addPlayerDetailViewController(controller: PlayerDetailViewController, didFinishEditingPlayer player: Player){
    // call sort of players after edit
    team.teamPlayers.sortPlayers() -> Value of type [Player] has no type 'sortPlayers'
    // reload the table
    tableView.reloadData()
    // dismiss the controller
    dismissViewControllerAnimated(true, completion: nil)
}

Shouldn’t that be team.sortPlayers() ? Since the sortPlayers method is part of your Team model?

How silly of me! Thanks :smile: