mustafa
mustafa

Reputation: 491

How to group Array of Dictionaries by a key in swift?

For example, I have this array of dictionaries

 [["Country":"Egypt","Name":"Mustafa","Age":"20"],["Country":"Palestine","Name":"Omar","Age":"15"],["Country":"Egypt","Name":"Ali","Age":"40"],["Country":"Jordan","Name":"Ahmad","Age":"25"],["Country":"Palestine","Name":"Amani","Age":"30"],["Country":"Jordan","Name":"Mustafa","Age":"20"]]

I want to group them by Country to become

  {"Egypt": [{"Country":"Egypt","Name":"Mustafa","Age":"20"} {"Country":"Egypt","Name":"Ali","Age":"40"}],
   "Palestine": [{"Country":"Palestine","Name":"Amani","Age":"30"},{"Country":"Palestine","Name":"Omar","Age":"15"}],
   "Jordan":[{"Country":"Jordan","Name":"Ahmad","Age":"25"},{"Country":"Jordan","Name":"Mustafa","Age":"20"}]
}

Please help.

Upvotes: 1

Views: 6830

Answers (3)

Fogmeister
Fogmeister

Reputation: 77661

Swift has a nice function that does this for you...

let people = [["Country":"Egypt","Name":"Mustafa","Age":"20"],["Country":"Palestine","Name":"Omar","Age":"15"],["Country":"Egypt","Name":"Ali","Age":"40"],["Country":"Jordan","Name":"Ahmad","Age":"25"],["Country":"Palestine","Name":"Amani","Age":"30"],["Country":"Jordan","Name":"Mustafa","Age":"20"]]

let peopleByCountry = Dictionary(grouping: people, by: { $0["Country"]! } )

peopleByCountry will now be the format that you want.

You can read more about this function in the documentation.

Just to add to Hamish's comment.

You really shouldn't be working with Dictionaries here. You should be working with Structs...

struct Person {
    let countryName: String
    let name: String
    let age: Int
}

Even better would be to have a Country struct...

struct Country {
    let name: String
}

and use that in the Person for their country property instead of String.

Upvotes: 6

Chanchal Warde
Chanchal Warde

Reputation: 983

let arrCountry: [[String:String]] = [["Country":"Egypt","Name":"Mustafa","Age":"20"],
                  ["Country":"Palestine","Name":"Omar","Age":"15"],
                  ["Country":"Egypt","Name":"Ali","Age":"40"],
                  ["Country":"Jordan","Name":"Ahmad","Age":"25"],
                  ["Country":"Palestine","Name":"Amani","Age":"30"],
                  ["Country":"Jordan","Name":"Mustafa","Age":"20"]]

func sortCountry() {
    var sortedCountries : [String : [[String:String]]] = [:]
    for object in arrCountry {
        let country = object["Country"] as! String
        if var arrCountry = sortedCountries[country] {
            arrCountry.append(object)
            sortedCountries[country] =  arrCountry
        }
        else {
            sortedCountries[country] =  [object]
        }
    }
}

Upvotes: 1

hardik parmar
hardik parmar

Reputation: 853

Well I would go like this:

  1. Get all the countries by traversing the array once and store it in an array.
  2. Loop for this array of countries.
  3. Filter the array with predicate where country is current country.
  4. Store this in the final dictionary of country.

Upvotes: -1

Related Questions