Min Htet Oo
Min Htet Oo

Reputation: 188

How to group array of objects and transform into another array of grouped objects

Let's say I have array of Report Objects and GroupedReport Objects This is my Report Class Structure

class Report {

    var report_date:String
    var problem:String
    var description:String
    var is_know:Int
    var is_solved:Int

    init(report_date:String,problem:String,description:String,is_know:Int,is_solved:Int) {
     self.report_date = report_date
     self.problem = problem
     self.description = description
     self.is_know = is_know
     self.is_solved = is_solved
    }
}

This is my GroupedReport Class structure

        class GroupedReport{
            var problem:String
            var report_array:[Report]

            init(problem:String){
                self.problem = problem
                report_array = []
            }

            public var description: String { return "GroupedReort:\nProblem:\(self

    .problem)\nArray:\(self.report_array)" }
}

Is there any algorithm to group Report objects with the same problem (class variable of Report) value and transform into GroupedReport objects? I have done the implementation of my own and my code is not working as I expected.Could someone help me please? Thanks for your attention

            var reports:[Report] = []
            var problem_array:[String] = []
            var grouped_reports: [GroupedReport] = []
    func group_array(){
 for i in 0...self.reports.count-1{
            print("\(i) Loop ::")
            print("============")
            var problem_found:Bool = false
            // j loop start
            if problem_array.count > 0 {
            for j in 0...self.problem_array.count-1{
                print("problem_array[j]: \(problem_array[j]) <compare to> reports[i].problem: \(reports[i].problem)")
                print("")
                if(problem_array[j] == reports[i].problem){
                    print("")
                    print("problem_array[j] \(problem_array[j]) <is equal to> reports[i].problem \(reports[i].problem)")
                    problem_found = true
                    // this is the existing problem
                }
            }
            }
            // j loop end
            if(problem_found){
                //find problem key and append array to that key
                for x in 0...self.grouped_reports.count-1{
                    if self.grouped_reports[x].problem == reports[x].problem{
                        print("")
                        print("Problem found")
                        print("Append Report problem :\(reports[x].problem)")
                        print("Append Report des :\(reports[x].description)")
                        self.grouped_reports[x].report_array.append(reports[x])
                    }
                }
            }
            else{
                // create new problem key 
                problem_array.append(reports[i].problem)
                //crete new group_report with new problem and append current report[i] to that report , append that group_report to the group_report array
                var group_report = GroupedReport(problem: reports[i].problem)
                group_report.report_array.append(reports[i])
                print("")
                print("new problem")
                    print("Append Report problem :\(reports[i].problem)")
                    print("Append Report des :\(reports[i].description)")

                self.grouped_reports.append(group_report)

            }

        }
            print("!!Final Array!!")
            print("=================")

            for i in 0...grouped_reports.count-1 {
            print("\(i) Array!")
            print("-----------")
            print("Problem:\(self.grouped_reports[i].problem)")
            print("Inner Array")
            print("count: \(grouped_reports[i].report_array.count)")
                for j in 0...grouped_reports[i].report_array.count-1{
                print(grouped_reports[i].report_array[j].description)
                }
            }
    }

Upvotes: 0

Views: 213

Answers (1)

simonWasHere
simonWasHere

Reputation: 1340

Use code like this to generate the grouped reports:

var groupedReports: [String: GroupedReport] = [:]

reports.forEach { report in
    if let groupedReport = groupedReports[report.problem] {
        groupedReport.report_array.append(report)
    } else {
        let groupedReport = GroupedReport(problem: report.problem)
        groupedReport.report_array.append(report)
        groupedReports[report.problem] = groupedReport
    }
}

Then you can loop through the results like this:

for (problem, groupedReport) in groupedReports {
    // do something with the groupedReport
}

update

Or convert the results to an array like this:

let grouped_reports = Array(groupedReports.values)

Upvotes: 2

Related Questions