I am want to display the three highest grades along with the names
My code:
let gradesofStudent = [
"Elias Brown": 80,
"Baris Yan": 99,
"Christian Najm": 70,
"Sam Chakra": 90,
"Alain Elliot": 55,
"Georges Obama": 77,
]
var y = " "
var name = " "
func mainFunction(stDictionary: Dictionary<String, Int>) -> ArraySlice<String>{
var emptyDic: [String: String] = [:]
for (keys, value) in gradesofStudent {
if value > 0 && value <= 59 {y = "F"}
else if value >= 60 && value <= 69 {y = "D"}
else if value >= 70 && value <= 79 {y = "C"}
else if value >= 80 && value <= 89 {y = "B"}
else {y = "A"}
emptyDic[keys] = y
}
var arrayGrades = (Array(emptyDic).sorted{$0.1 < $1.1}).map{(k,v) in return(k:v)}
var firstThree = arrayGrades[0..<3]
return(k: firstThree)
}
let v = mainFunction(stDictionary:gradesofStudent)
print (v)
I am getting the following output:
["A", "A", "B"]
What I want to get is something like [ "Baris Yan":"A", "Sam Chakra":"A", "Elias Brown":"B"]
Since i can't seem to sort the dictionary directly, I can't figure how to display the names.
struct Studentwithvar name : String,var grade : Intmembers, computed propertyvar letterRepresentation : String { ...}and an array of instances. Btw: A dictionary is unordered by definition.