如何在swift中对多维数组进行排序?

时间:2016-04-19 11:38:49

标签: ios arrays swift multidimensional-array

我需要你的帮助。我做了一个数组,当我在不同的设备或模拟器上启动我的应用程序时,这个数组分为不同的类型,但我需要在每个设备上使用相同的顺序。怎么做?

    var settings = [String: [ [String: String] ]]()
    settings = [ "cards":[ ], "groups":[ ] ]

    ...

    for card in db.prepare(table.order(orderN.asc)) {
       settings["cards"]?.append(["id":String(card[id]), "group_id":String(card[group_id]), "service":card[service], "bgcolor":card[bgcolor], "logoimg":card[logoimg]!, "balance_desc":card[balance_desc], "balance":"0.0", "balance_id":String(card[balance_id]), "uniqueID":card[uniqueID], "balance_currency":String(card[balance_currency]), "orderN":String(card[orderN])])
    }

    ...

    for group in db.prepare(table.order(orderN.asc)) {  
       settings["groups"]?.append(["name":group[name]!,"id":String(group[id]),"orderN":String(group[orderN])])
    }

    ...

例如, 在第一台设备上

print(settings) // ["cards": [["orderN": "0", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance": "0.0", "logoimg": "example.com/images/img.png", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "group_id": "2", "id": "1", "service": "servicename", "balance_desc": "Description", "balance_id": "1", "balance_currency": "1"]], "groups": [["orderN": "0", "name": "GroupName", "id": "2"]]]

在第二台设备上

print(settings) // ["cards": [["orderN": "0", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "service": "servicename", "id": "1", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance_currency": "1", "balance": "0.0", "group_id": "2", "logoimg": "example.com/images/img.png", "balance_id": "1", "balance_desc": "Description"]], "groups": [["id": "2", "orderN": "0", "name": "GroupName"]]]

感谢您的关注。

1 个答案:

答案 0 :(得分:0)

那是因为settings不是Array,而是Dictionary。未定义字典中键值对的顺序。

如果您需要某个特定订单,则应重新实现settings,可能会将它们分开structclass,因为您将很难使用嵌套词典。