Swift sort dictionary by value

前端 未结 3 994
眼角桃花
眼角桃花 2020-12-18 12:29

I have a dictionary, [String : Double] with the following data:

Museum1 : 8785.8971799638
Museum2 : 34420.9643422388
Museum3 : 826.467789130732
         


        
3条回答
  •  猫巷女王i
    2020-12-18 13:25

    In Swift 2.2, this works

    for (k,v) in (Array(d).sort {$0.1 < $1.1}) {
        print("\(k):\(v)")
    }
    

    Swift 3, it also works:

    for (k,v) in (Array(yourDictionary).sorted {$0.1 < $1.1}) {
        print("\(k):\(v)")
    }
    

提交回复
热议问题