Swift array Group Data Adapter


Have you ever face challenges in searching and sorting grouped data? If your answer is

  • Yes — No, worries we will learn a better way to handle it in this article.
  • No — That’s good, You might learn something.

GroupDataAdapter is a generic class that accepts a group keys array and items dictionary. That’s it. This helper class will manage group sorting, group item sorting, and item filtering.

The main usage of this adapter class is to maintain a clean architecture, No conditional work, or flag work. And more importantly, it’s plug and surf class that works with all data types.

Let’s understand using one example. Here one WebSeires model with a data loading method from the JSON file.

struct WebSeires : Codable {
let name : String
let imageName : String
let rating : Double
let genre : [String]
let numberOfSeasons : Int
let description : String
//MARK:- JSON file loading
extension WebSeires {
static func loadUsingJSON() -> [WebSeires] {

guard let resourceUrl = Bundle.main.url(forResource: "response", withExtension: "json") else {

return []

do {
let jsonData = try Data.init(contentsOf: resourceUrl)
let tempalte = try JSONDecoder().decode([WebSeires].self, from: jsonData)
return tempalte
} catch {
print("Decoding Fail ==::\(error)")
return []
extension WebSeires {var group : String {return String("Rating : \(rating)")}}

Now, We have array data that can load from WebSeires.loadUsingJSON(). And let’s say we want to group our web series data according to the rating of the series. So, we need a rating key for our group.

extension WebSeires {
var group : String {
return String("Rating: \(rating)")

In the above WebSeires extension group key generated from web series rating and “Rating:” text. Group key can be any existing hashable property or calculated property.

let groups = WebSeires.loadUsingJSON().adapterGrouping { (seires) -> String in
return seires.group
lazy var adapter : GroupedDataAdapter = {
GroupedDataAdapter(groups: groups.keys, items: groups.values)
adapter.groupSortComparator = { lhs, rhs in
return lhs > rhs
adapter.itemSortComparator = { lhs, rhs in
return lhs.name < rhs.name
adapter.filterBlock = { items in
return items.filter { (seires) -> Bool in
return seires.rating > 8.0
print("Groups : \(adapter.operationalGroups) - Items : \(adapter.operationalItems)")
  1. Creating groups key and values dictionary using adapterGrouping sequence helper method. adapterGrouping returns a tuple value of keys and dictionary.
  2. GroupDataAdapter instance creating using Keys and Dictionary.
  3. Applied sorting order to group ordering.
  4. Applied sorting order to group items ordering.
  5. Applied filtering over adapter data.
  6. The immediate effect of sorting and filtering will be reflected in the adapter.operationalGroups and adapter.operationalItems.

To know more about sorting comparator, reusable comparator, or remove sorting or filtering checkout ArrayDataAdapter.

Checkout full demo from here

Stay tuned… for more plug and surf class.


Software developer at Spaceo Technologies. Loves to create iOS apps. Github : https://github.com/Pratik-Sodha