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

时间:2022-05-06 15:58:03

I need your help. I did an array, and when I launch my application on different devices or simulator, this array sorted into different types, but I need the same order on each device. How to do that?

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

    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])])
    }

    ...

For example, On the first device

例如,在第一台设备上

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"]]]

On the second device

在第二个设备上

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"]]]

Thank you for you attention.

谢谢你的关注。

1 个解决方案

#1


0  

That's because settings is not an Array, it's Dictionary. Order of key-value pairs in dictionary is not defined.

那是因为设置不是数组,而是字典。未定义字典中键值对的顺序。

If you need some particular order, you should reimplement settings, probably make them separate struct or class, because you'll have a hard time working with nested dictionaries.

如果你需要一些特定的顺序,你应该重新实现设置,可能使它们成为单独的结构或类,因为你将很难使用嵌套的字典。

#1


0  

That's because settings is not an Array, it's Dictionary. Order of key-value pairs in dictionary is not defined.

那是因为设置不是数组,而是字典。未定义字典中键值对的顺序。

If you need some particular order, you should reimplement settings, probably make them separate struct or class, because you'll have a hard time working with nested dictionaries.

如果你需要一些特定的顺序,你应该重新实现设置,可能使它们成为单独的结构或类,因为你将很难使用嵌套的字典。