Mujtaba
Mujtaba

Reputation: 97

How to sort two Array and add common element into third one in swift

I am new in swift and I am not able to get common array

"certification": [
            {
                "certification_id": "1",
                "certification_name": "ug"
            },
            {
                "certification_id": "2",
                "certification_name": "pg"
            },
            {
                "certification_id": "3",
                "certification_name": "academy"
            }
        ],
        "specialization": [
            {
                "specialisation_id": "1",
                "specialisation_name": "Fashion Design",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "2",
                "specialisation_name": "Interior Design",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "3",
                "specialisation_name": "Communication Design",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "4",
                "specialisation_name": "Product Design",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "5",
                "specialisation_name": "Strategic Design and Management",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "6",
                "specialisation_name": "Fashion Communication & Styling",
                "specialisation_short_name": "UG",
                "specialisation_school": "1",
                "specialisation_certification": "1"
            },
            {
                "specialisation_id": "7",
                "specialisation_name": "Design and Innovation",
                "specialisation_short_name": "PG",
                "specialisation_school": "1",
                "specialisation_certification": "2"
            }
           ]

if I select "certification_id": "2" then I will get only data of "specialisation_certification": "2"

But I need the whole data of array like

"specialisation_id": "7",
"specialisation_name": "Design and Innovation",
"specialisation_short_name": "PG",
"specialisation_school": "1",
"specialisation_certification": "2"

It could also be multiple select like certification_id can be 1,2

Is it possible if yes the how can I do it. Thanks in Advance!

I tried this code

var list:Array<Int> = [1,2,3,4,5]
var findList:Array<Int> = [1,3,5]

let listSet = NSSet(array: list)
let findListSet = NSSet(array: findList)

let allElemtsEqual = findListSet.isSubsetOfSet(otherSet: listSet)

But it only gives me values in true or false but I need all values

I tried @PGDev code its working but only when its equal. I need to implement this on textfield

  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

        if textField == txtSearch{
            let certificationId = "800016"
            print(certificationId)

            let result = (searchdict["specialization"] as? [[String:Any]])?.filter({ ($0["specialisation_id"] as? String) == certificationId })
            print("Result = ",result as Any)


        }

    return true
}

Upvotes: 0

Views: 98

Answers (2)

abanoub nabil
abanoub nabil

Reputation: 81

use this code snippet to get common items between the tow arrays based on the id .
var specializations: [Specialization]? var certifications: [Certification]? var filtered: [Certification]?

specializations?.forEach({ (specification) in filtered?.append(contentsOf: (certifications?.filter({specification.specialisation_id == $0.certification_id}))!) })

Upvotes: 1

PGDev
PGDev

Reputation: 24341

If the dict is

let dict = [
    "certification": [
        ...
    ],
    "specialization": [
        ...
    ]
]

Then you can get the whole specialization dict based on certificationId like so,

let certificationId = "2"

let result = (dict["specialization"] as? [[String:Any]])?.filter({ ($0["specialisation_certification"] as? String) == certificationId })

print(result) //[["specialisation_name": "Design and Innovation", "specialisation_id": "7", "specialisation_certification": "2", "specialisation_school": "1", "specialisation_short_name": "PG"]])

Upvotes: 0

Related Questions