ranking list
๐ replit
// name and score
let dict = [ // Dictionary<Key, Value>
"Sam": 79, "Joe": 5, "Mary": 79, // Key = String
"Tom": 5, "Alex": 82, "Nancy": 1 // Value = Int
]
// โญ๏ธ 1. sort it by value first
let sorted = dict.sorted { $0.value > $1.value }
var prev: Int? // previous score
var currentRank = 0
var rankingList = [(name: String, score: Int, rank: Int)]()
// โญ๏ธ 2. give every element a rank
for (i, elem) in sorted.enumerated() {
// if elem.value is not the same as previous score,
// update previous score and current rank
if prev == nil || elem.value != prev! {
prev = elem.value
currentRank = i + 1 // i is zero-based, rank is one-based
}
rankingList.append((elem.key, elem.value, currentRank))
}
print(rankingList)
/*
[
(name: "Alex" , score: 82, rank: 1),
(name: "Sam" , score: 79, rank: 2),
(name: "Mary" , score: 79, rank: 2), // no 3rd place
(name: "Joe" , score: 5, rank: 4),
(name: "Tom" , score: 5, rank: 4), // no 5th place
(name: "Nancy", score: 1, rank: 6)
]
*/
๐ paiza.io
let dict = [ // Dictionary<Key, Value>
"Sam": 79, "Joe": 5, "Mary": 18, // Key = String
"Tom": 5, "Alex": 82, "Nancy": 1 // Value = Int
] // Element = (key: Key, value: Value)
// โญ๏ธ ๆ็
งใๅๆธใๅ็ต
let dict2 = dict.grouped { $0.value } // seq.grouped(by:) -> ๐ฅ ็ธ้
/*
โญ๏ธ seq.grouped(by:)
--------------------------------------
Sequence -> Dictionary<Key, [Element]>
ๅ
ถไธญ Key ็บๅๆธ by: (Element) -> Key ๆๆๅฎใ
[ Key โญโโโ Value = [Element] โโโโฎ
79: [(key: "Sam" , value: 79)],
5: [(key: "Tom" , value: 5) , (key: "Joe", value: 5)],
82: [(key: "Alex" , value: 82)],
18: [(key: "Mary" , value: 18)],
1: [(key: "Nancy", value: 1)]
]
*/
// โญ๏ธ ๆ็
งใๅๆธใ็ฑๅคงๅฐๅฐๆๅ
let sorted = dict2.sorted { $0.key > $1.key }
/*
โญ๏ธ seq.sorted(by:)
---------------------
Sequence -> [Element]
ไธๅ Dictionary ็ Element = (key: Key, value: Value)๏ผ
ไฝ็พๅจ็ Key = Int๏ผValue = [(key: String, value: Int)]๏ผ
ๆไปฅ็ถ้ .sorted(by:) ไนๅพ่ช็ถๆ่ฎๆ๏ผ
[(key: Int, value: [(key: String, value: Int)])]
[
(key: 82, value: [(key: "Alex" , value: 82)]),
(key: 79, value: [(key: "Sam" , value: 79)]),
(key: 18, value: [(key: "Mary" , value: 18)]),
(key: 5, value: [(key: "Tom" , value: 5), (key: "Joe", value: 5)]),
(key: 1, value: [(key: "Nancy", value: 1)])
]
*/
// โญ๏ธ ๅนซ Sequence ็ทจ่
let enumerated = sorted.enumerated()
/*
โญ๏ธ seq.enumerated()
------------------------------------------
Sequence -> EnumeratedSequence<[Element]>
ๅนซ Sequence ็ทจ่๏ผ็ทจ่ๅพๅฏ็จไธๅ่ชๆณไพ่็ๆฏๅๅ
็ด ๏ผ
for (i, elem) in seq.enumerated() { ... }
ๆไปฅๅจๆฌไพไธญ๏ผๅฏไปฅๅๆณๆ็ถ้ .enumerated() ่็ๅพ๏ผ่ฎๆ้ๆจฃ๏ผ
[
(0, (key: 82, value: [(key: "Alex" , value: 82)])),
(1, (key: 79, value: [(key: "Sam" , value: 79)])),
(2, (key: 18, value: [(key: "Mary" , value: 18)])),
(3, (key: 5, value: [(key: "Tom" , value: 5), (key: "Joe", value: 5)])),
(4, (key: 1, value: [(key: "Nancy", value: 1)]))
]
*/
// โญ๏ธ ๅฐ elem ๅ
็ด ๅ
ง้จ็ๅผ(elem.value)่ฝ่ฎ็บ้ฃๅ๏ผ็ถๅพๅจ็จ .flatMap()
// ๅฐๆๆ้ฃๅใๅฃๆใไธ็ถญ้ฃๅใ
let ranking = enumerated.flatMap { (i, elem) in
elem.value.map { (name: $0.key, rank: i + 1, value: $0.value) }
}
/*
[
(name: "Alex" , rank: 1, value: 82),
(name: "Sam" , rank: 2, value: 79),
(name: "Mary" , rank: 3, value: 18),
(name: "Tom" , rank: 4, value: 5),
(name: "Joe" , rank: 4, value: 5),
(name: "Nancy", rank: 5, value: 1)
]
*/
Swift ranking dictionary - build ranking list from scores
Last updated