2017-02-16 221 views
0

我有一個字典數組。我需要對這個數組進行排序。排序不應該像升序或降序,但它應該基於另一個數組內容。 EX:假設我有一個數組nammed array_unsorted,該數組包含許多字典對象,如d1,d2,d3,d4等。每個字典對象都有一個名爲key1的鍵,並且每個字典對象對於該鍵具有不同的值如KammyMaddy,Jessy。可以說我有anohter排序數組,其中Maddy,Kammy,Jessy。現在字典應該按照第一個元素應該是字典對象的方式排序,其中的值爲key1 should be Maddy。如何根據Swift中的數組內容對字典數組進行排序

我無法使用SortDescriptor,因爲這將根據傳遞給它的鍵以升序或降序排序。

我試過我的解決方案,但我最終使用瞭如此多的嵌套循環。我覺得我的解決方案非常可憐,我甚至不想在這裏發佈代碼。

任何幫助將非常感激。

編輯:可以有多個排序數組,但截至目前我正在考慮只有一個排序數組,然後我可以編寫多個排序數組的代碼。

+1

分享一些相關的代碼會更清晰可讀:) – ystack

+0

@iOS_Developer給我們一個可以編輯的輸入和輸出的例子,您可以期待它。 – Alexander

回答

1

如何:

創建一個新的空字典有String鍵,並輸入字典的價值。稱它爲sourceItemsDict

循環遍歷源數組中的字典,並將每個條目添加到新字典中,使用排序鍵作爲字典鍵,並將數組條目作爲值。

爲您的排序結果創建一個新的,空的字典數組。稱它爲sortedArray

現在循環遍歷您的數組,它具有所需的順序。使用該密鑰從sourceItemsDict中獲取項目並將其附加到sortedArray的末尾。

應該這樣做,它應該在O(n)時間內執行。

0

看看這個例子:

let dic1 = ["name" : "a"] 
let dic2 = ["name" : "b"] 
let dic3 = ["name" : "c"] 
let dic4 = ["name" : "d"] 
let dic5 = ["name" : "e"] 


let unsorted_array = [dic2,dic5,dic1,dic4,dic3] 

func sortArrayByName(_ array:[[String:String]])->[[String:String]]{ 
    var sortedArray:[[String:String]] = [[String:String]]() 
    var sortingOrder:[String] = [String]() 
    for eachDic in array{ 
     if let name = eachDic["name"]{ 
      sortingOrder.append(name) 
      sortingOrder.sort() // sorting logic here 

      if sortedArray.isEmpty{ 
       sortedArray.append(eachDic) 
      } else { 
       let index = sortingOrder.index(of: name)! 
       sortedArray.insert(eachDic, at: index) 
      } 
     } 
    } 

    return sortedArray 
} 

let sorted_array = sortArrayByName(unsorted_array) 
+0

這並沒有解決OP問題。他們想對字典數組進行排序,以便字典中排序鍵的順序與字符串數組匹配。 –

1

試試這個:

func sort<T: Equatable>(arrayOfDict arr: [[String: T]], by key: String, order: [T]) -> [[String: T]] { 
    return arr.sorted { 
     guard let value0 = $0[key], let value1 = $1[key] else { 
      return false 
     } 
     guard let index0 = order.index(of: value0), let index1 = order.index(of: value1) else { 
      return false 
     } 

     return index0 < index1 
    } 
} 

let array_unsorted = [ 
    ["name": "Kammy", "city": "New York"], 
    ["name": "Maddy", "city": "Cupertino"], 
    ["name": "Jessy", "city": "Mountain View"] 
] 
let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"]) 
let sortedByCity = sort(arrayOfDict: array_unsorted, by: "city", order: ["Cupertino", "Mountain View", "New York"]) 

print(sortedByName) 
print(sortedByCity) 

你的問題留下了幾個懸而未決的情景:

1:如果什麼密鑰丟失從字典?

let array_unsorted = [ 
    ["name": "Kammy", "city": "New York"], 
    ["city": "Las Vegas"], 
    ["name": "Maddy", "city": "Cupertino"], 
    ["name": "Jessy", "city": "Mountain View"] 
] 

let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"]) 

拉斯維加斯應該出現在排序數組的開始還是結束?

2:如果您未指定某個值的訂單,該怎麼辦?

let array_unsorted = [ 
    ["name": "Amy"], 
    ["name": "Kammy", "city": "New York"], 
    ["name": "Maddy", "city": "Cupertino"], 
    ["name": "Jessy", "city": "Mountain View"] 
] 

let sortedByName = sort(arrayOfDict: array_unsorted, by: "name", order: ["Maddy", "Kammy", "Jessy"]) 

現在應該在哪放置Amy

+0

非常感謝回覆。這已經解決了我的一半問題。作爲未指定的scnarios的一部分,「拉斯維加斯」應該結束,沒有關鍵的失蹤。並且它將它修復了一半,因爲下一級排序(在這種情況下爲城市)應該應用於已排序的(基於名稱)數組。再次感謝。 –

相關問題