sort two completly different type of array with one common properties












-3















Hi I have two array with different properties and only properties has common sort_order and I want to merge this two array and want to sort this two array by sort_order.



Here is the code:



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









share|improve this question

























  • if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

    – Rakesha Shastri
    Nov 26 '18 at 10:21













  • because I already created model classes for this objects

    – Nirav Hathi
    Nov 26 '18 at 10:24











  • For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

    – Rakesha Shastri
    Nov 26 '18 at 10:25













  • you should be able to achieve that using protocol

    – kathayatnk
    Nov 26 '18 at 10:34
















-3















Hi I have two array with different properties and only properties has common sort_order and I want to merge this two array and want to sort this two array by sort_order.



Here is the code:



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









share|improve this question

























  • if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

    – Rakesha Shastri
    Nov 26 '18 at 10:21













  • because I already created model classes for this objects

    – Nirav Hathi
    Nov 26 '18 at 10:24











  • For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

    – Rakesha Shastri
    Nov 26 '18 at 10:25













  • you should be able to achieve that using protocol

    – kathayatnk
    Nov 26 '18 at 10:34














-3












-3








-3








Hi I have two array with different properties and only properties has common sort_order and I want to merge this two array and want to sort this two array by sort_order.



Here is the code:



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









share|improve this question
















Hi I have two array with different properties and only properties has common sort_order and I want to merge this two array and want to sort this two array by sort_order.



Here is the code:



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






arrays swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 11:23









kit

1,10631017




1,10631017










asked Nov 26 '18 at 10:18









Nirav HathiNirav Hathi

4351518




4351518













  • if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

    – Rakesha Shastri
    Nov 26 '18 at 10:21













  • because I already created model classes for this objects

    – Nirav Hathi
    Nov 26 '18 at 10:24











  • For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

    – Rakesha Shastri
    Nov 26 '18 at 10:25













  • you should be able to achieve that using protocol

    – kathayatnk
    Nov 26 '18 at 10:34



















  • if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

    – Rakesha Shastri
    Nov 26 '18 at 10:21













  • because I already created model classes for this objects

    – Nirav Hathi
    Nov 26 '18 at 10:24











  • For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

    – Rakesha Shastri
    Nov 26 '18 at 10:25













  • you should be able to achieve that using protocol

    – kathayatnk
    Nov 26 '18 at 10:34

















if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

– Rakesha Shastri
Nov 26 '18 at 10:21







if you merge them both, how will you know which is an object of array1 and which is that of array2 when you use it? or is it like this: name from array1 is the same as group from array2?

– Rakesha Shastri
Nov 26 '18 at 10:21















because I already created model classes for this objects

– Nirav Hathi
Nov 26 '18 at 10:24





because I already created model classes for this objects

– Nirav Hathi
Nov 26 '18 at 10:24













For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

– Rakesha Shastri
Nov 26 '18 at 10:25







For objects to be in the same array, they all have to be of the same type. So, if they are of the same type, say Any, how would you know which object is of which type when you want to access them from the said array.

– Rakesha Shastri
Nov 26 '18 at 10:25















you should be able to achieve that using protocol

– kathayatnk
Nov 26 '18 at 10:34





you should be able to achieve that using protocol

– kathayatnk
Nov 26 '18 at 10:34












4 Answers
4






active

oldest

votes


















1














Ah, I guess, in good old Objective-C days, we could have used a sort predicate.
Well, those days are behind us and protocols are ministers in new Swift government.



Lets see what protocols can do in this case.

You could define a protocol that has sort_order as a property.
This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic).

Then you can create a merged array of type Ordered, and use sort function on that array.



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
}





share|improve this answer

































    0














    Use this



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





    share|improve this answer































      0














      Here how you can do it using swift 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"]]





      share|improve this answer































        0














        You can try like this



            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)





        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478980%2fsort-two-completly-different-type-of-array-with-one-common-properties%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Ah, I guess, in good old Objective-C days, we could have used a sort predicate.
          Well, those days are behind us and protocols are ministers in new Swift government.



          Lets see what protocols can do in this case.

          You could define a protocol that has sort_order as a property.
          This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic).

          Then you can create a merged array of type Ordered, and use sort function on that array.



          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
          }





          share|improve this answer






























            1














            Ah, I guess, in good old Objective-C days, we could have used a sort predicate.
            Well, those days are behind us and protocols are ministers in new Swift government.



            Lets see what protocols can do in this case.

            You could define a protocol that has sort_order as a property.
            This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic).

            Then you can create a merged array of type Ordered, and use sort function on that array.



            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
            }





            share|improve this answer




























              1












              1








              1







              Ah, I guess, in good old Objective-C days, we could have used a sort predicate.
              Well, those days are behind us and protocols are ministers in new Swift government.



              Lets see what protocols can do in this case.

              You could define a protocol that has sort_order as a property.
              This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic).

              Then you can create a merged array of type Ordered, and use sort function on that array.



              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
              }





              share|improve this answer















              Ah, I guess, in good old Objective-C days, we could have used a sort predicate.
              Well, those days are behind us and protocols are ministers in new Swift government.



              Lets see what protocols can do in this case.

              You could define a protocol that has sort_order as a property.
              This protocol needs to be conformed by both your object types (A and B in below code. Also, made them Codable for yet more protocol magic).

              Then you can create a merged array of type Ordered, and use sort function on that array.



              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
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 26 '18 at 10:44

























              answered Nov 26 '18 at 10:35









              Puneet SharmaPuneet Sharma

              7,86011928




              7,86011928

























                  0














                  Use this



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





                  share|improve this answer




























                    0














                    Use this



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





                    share|improve this answer


























                      0












                      0








                      0







                      Use this



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





                      share|improve this answer













                      Use this



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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 26 '18 at 10:34









                      dahiya_boydahiya_boy

                      5,54511031




                      5,54511031























                          0














                          Here how you can do it using swift 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"]]





                          share|improve this answer




























                            0














                            Here how you can do it using swift 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"]]





                            share|improve this answer


























                              0












                              0








                              0







                              Here how you can do it using swift 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"]]





                              share|improve this answer













                              Here how you can do it using swift 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"]]






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 26 '18 at 10:36









                              NivNiv

                              317211




                              317211























                                  0














                                  You can try like this



                                      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)





                                  share|improve this answer




























                                    0














                                    You can try like this



                                        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)





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      You can try like this



                                          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)





                                      share|improve this answer













                                      You can try like this



                                          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)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 26 '18 at 10:38









                                      Mahesh MMahesh M

                                      357




                                      357






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53478980%2fsort-two-completly-different-type-of-array-with-one-common-properties%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin