0

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

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

5 Comments

edited to add the first line which I missed off at first
Thanks sir..you code really works!!.How can I access this dictionary by index value ( indexPath.row and indexPath.section )to show in my table view ( viewForHeaderInSection method and cellForRowAt indexPath method).Thanks for your attention
If you prefer the results in an array for easy access like that you can write: let reportArray = Array(groupedReports.values)
I have constructed another class initializer ,change your code and transform into my grouped_reports array like this. for (problem, groupedReport) in groupedReports { self.grouped_reports.append(GroupedReport(problem:problem , report_array:groupedReport.report_array)) }
Because I designed my header views and cells with that class data structure .. ( header.headerReason_label.text = self.grouped_reports[section].problem in viewForHeaderInSection method and cell.message_Label.text = self.grouped_reports[indexPath.section].report_array[indexPath.row].description in cellForRowAt indexPath method ) Thank you very much.You really saved my day :) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.