用一个公共属性对两个完全不同类型的数组进行排序

时间:2018-11-26 10:18:49

标签: arrays swift

您好,我有两个具有不同属性的数组,只有属性具有相同的sort_order,并且我想合并这两个数组并想按sort_order对这两个数组进行排序。

代码如下:

var array1:[AnyObject] = [{
    "name":"obj1Array1"
    "sort_order": 4
    "type":"G"
},{
    "name":"obj2Array1"
    "sort_order": 1
    "type":"G"
},{
    "name":"obj31Array1"
    "sort_order": 5
    "type":"G"
}]

var array2:[AnyObject] = [{
    "Group":"M"
    "sort_order": 3
    "type":"G"
},{
    "Group":"C"
    "sort_order": 2
    "type":"G"
},{
    "Group":"D"
    "sort_order": 6
    "type":"G"
}]

4 个答案:

答案 0 :(得分:1)

嗯,我想在Objective-C的旧时光中,我们本可以使用排序谓词。 好吧,那些日子已经过去了,礼节是斯威夫特新政府的部长。

让我们看看在这种情况下协议可以做什么。
您可以定义一个具有sort_order属性的协议。 该协议需要同时符合您的两种对象类型(以下代码中的A和B。此外,使它们成为可编码的还可以实现更多的协议魔术)。
然后,您可以创建Ordered类型的合并数组,并在该数组上使用sort函数。

protocol Ordered {
    var sort_order:Int{get}
}

struct A:Codable, Ordered {
    let name:String
    let sort_order:Int
    let type:String
}

struct B:Codable, Ordered {
    let Group:String
    let sort_order:Int
    let type:String
}

var array1:[A] = []
var array2:[B] = []

var mergedArray:[Ordered] = array1
for item in array2 {
    mergedArray.append(item)
}

mergedArray.sort { (A, B) -> Bool in
    return A.sort_order < B.sort_order
}

答案 1 :(得分:0)

使用此

    var array1 : [[String : Any]] = [[  "name" : "obj1Array1", "sort_order" : 4, "type":"G" ],
                  [ "name":"obj2Array1","sort_order": 1,"type":"G"],
                  ["name":"obj31Array1","sort_order": 5,"type":"G"]]

    var array2 : [[String : Any]] = [[ "Group":"M", "sort_order": 3,"type":"G"],
                  ["Group":"C", "sort_order": 2,"type":"G"],
                  ["Group":"D","sort_order": 6,"type":"G"]]



    let newArr = [array1, array2].flatMap({$0}).sorted(by: { ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int) })

答案 2 :(得分:0)

在这里,您如何使用快捷键4-

 var array1: [[String: Any]] = [
        ["name": "obj1Array1", "sort_order": 4, "type": "G"],
        ["name": "obj2Array1", "sort_order": 1, "type": "G"],
        ["name": "obj3Array1", "sort_order": 5, "type": "G"]
    ]

    var array2: [[String: Any]] = [
        ["Group": "M", "sort_order": 3, "type": "G"],
        ["Group": "C", "sort_order": 2, "type": "G"],
        ["Group": "D", "sort_order": 6, "type": "G"]
    ]

    let mergedArray = array1 + array2

    let sortedAndMerged = mergedArray.sorted{ ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int) }

    print(sortedAndMerged)

// prints [["type": "G", "name": "obj2Array1", "sort_order": 1], ["Group": "C", "sort_order": 2, "type": "G"], ["Group": "M", "sort_order": 3, "type": "G"], ["type": "G", "name": "obj1Array1", "sort_order": 4], ["type": "G", "name": "obj3Array1", "sort_order": 5], ["Group": "D", "sort_order": 6, "type": "G"]]

答案 3 :(得分:0)

您可以尝试这样

    let array1: [[String:Any]] = [ [
            "name":"obj1Array1",
            "sort_order": 4,
            "type":"G"
        ],
        [
            "name":"obj2Array1",
            "sort_order":1,
            "type":"G"
        ],
        [
            "name":"obj31Array1",
            "sort_order":5,
            "type":"G"

        ]]
    let array2: [[String:Any]] = [ [
            "Group":"M",
            "sort_order": 3,
            "type":"G"
        ],
        [
            "Group":"C",
            "sort_order": 2,
            "type":"G"
        ],
        [
            "Group":"D",
            "sort_order": 6,
            "type":"G"
        ]
    ]

    var new = array1 + array2
    new = new.sorted{
        ($0["sort_order"] as! Int) < ($1["sort_order"] as! Int)

        }

    print(new)