[ iOS _ Swift 學習筆記 ] Dictionary 取值排序 (use Dictionary key value)

Swift DictionAry 取值排序

近期在開發專案的時候,遇到一個問題...
就是在取出 JSON 格式的物件時候,每一次取出來的時候是不會按照順序的,
因此在於資料的處理上就會變成,每次放的位置順序不一至。
所以在把拿到的 JSON 資料轉成物件存放記憶體後,再處理排序的問題。


解決方式是透過幫 Dictionary 排序來完成。

因此在快速 google 後參考了別人的文章後解決這個,
Dictionary key 值排序的問題。

主要是透過以下這個方法

 public func sorted(by areInIncreasingOrder: ((key: Key, value: value), (key: key, value: Value)) -> Bool ->[(key: Key, value: Value)]


這樣就能順利拿到排序好的 Dictionary 值了。
然後 Swift 3 和 Swift 4 的語法有變動。
因此在 firstKeyRealValue 和 secondKeyRealValue 
會和參考的文章有所不同。

參考資料來源

Developmnt of the app have a question.
When i use JSON get objuct; Every time can't get same order.
So dispose data  first need get JSON data change objuct put in memory and  second solve order.

But i get solve question means is use Dictionary.
so i goole other people solve.
Use Dictionary key value solve.


 public func sorted(by areInIncreasingOrder: ((key: Key, value: value), (key: key, value: Value)) -> Bool ->[(key: Key, value: Value)]

留言

這個網誌中的熱門文章

IOS_Objective-C學習筆記_(陣列 / 可修改陣列 ;字典 / 可修改字典)..

20170122 Radiobutton功能練習 (溫度轉換)

iOS_開發 拿到 Tableview Cell 上的按鈕是哪一個 (Tableview Cell Button)