简体   繁体   English

如何快速排序多维数组?

[英]How to sort multidimensional array in swift?

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. 谢谢您的关注。

That's because settings is not an Array , it's Dictionary . 那是因为settings不是Array ,而是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. 如果您需要一些特定的命令,则应该重新实现settings ,可能将它们settings为单独的structclass ,因为使用嵌套字典会很困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM