Hi all,
I am storing different user data in a dictionary with a String (playerID) as the key, and an Int (ranking number) as the value. I have multiple dictionaries for 4-5 players. Each player will have their own dictionary allocated to them with their ranking and playerID as the key. I then want to sort the player's by value (ranking) so that I can get the highest ranked players ranking and playerID. I think the best way to do this is by storing all the dictionaries in an array? I am a little confused, but this is what I have so far.
var allPlayersArray = [[String:Int]]()
var player1DataDict = [String:Int]()
player1DataDict.updateValue(2, forKey: player1IDString)
var player2DataDict = [String:Int]()
player2DataDict.updateValue(1, forKey: player2IDString)
var player3DataDict = [String:Int]()
player3DataDict.updateValue(3, forKey: player3IDString)
allPlayersArray.append(player1DataDict)
allPlayersArray.append(player2DataDict)
allPlayersArray.append(player3DataDict)
//Do some sort of sorting to allPlayersArray??I want to sort allPlayersArray based on ranking so that I can do something like:
var highestRankedPlayer = allPlayersArray[0] as DictionaryThis way I can get the ranking and playerID from the variable highestRankedPlayer, in this case it would return player2DataDict as his ranking value is 1.
Sorry just tried OOPER's technique and it's telling me that [String:Int] from 'allPlayersRanking' does not have a member named 'sort'
I checked my code only in the latest version of Xcode 7, while I couldn't find what version of Xcode you are using.
If you are using Xcode 6.4, you need to change the line as:
let sortedRanking = sorted(allPlayersRanking){$0.1 < $1.1} //<-Swift 1.2But, as Jens already suggested, you'd better have a Player class or struct.
EDIT: A simple example using Player class, which runs on Xcode 6.4 playground.
class Player {
let id: String
var rank: Int
var position: CGPoint = CGPoint(x: 0, y: 0)
func distanceTo(point: CGPoint) -> CGFloat {
let dx = position.x - point.x
let dy = position.y - point.y
return sqrt(dx * dx + dy * dy)
}
init(id: String, rank: Int) {
self.id = id
self.rank = rank
}
}
let allPlayers = [
Player(id: "Foo", rank: 2),
Player(id: "Bar", rank: 1),
Player(id: "Baz", rank: 3)
]
allPlayers[0].position = CGPoint(x: 100, y: 100)
allPlayers[1].position = CGPoint(x: 110, y: 100)
allPlayers[2].position = CGPoint(x: -10, y: -10)
var startingPoint = CGPoint(x: 0, y: 0)
let sortedByDistance = sorted(allPlayers){$0.distanceTo(startingPoint) < $1.distanceTo(startingPoint)}
println(sortedByDistance[0].id) //->Baz
let sortedByRank = sorted(allPlayers){$0.rank < $1.rank}
println(sortedByRank[0].id) //->Bar
let sortedByIdDesc = sorted(allPlayers){$0.id > $1.id}
println(sortedByIdDesc[0].id) //->FooADDITION2: If you can use Swift2, and you just need only top one player, using minElement or maxElement is more efficient.
let topByDistance = allPlayers.minElement{$0.distanceTo(startingPoint) < $1.distanceTo(startingPoint)}!
print(topByDistance.id) //->Baz
let topByRank = allPlayers.minElement{$0.rank < $1.rank}!
print(topByRank.id) //->Bar
let bottomById = allPlayers.maxElement{$0.id < $1.id}!
print(bottomById.id) //->Foo