Swift iOS - 将数组过滤为类似对象的数组

时间:2016-01-13 20:24:56

标签: ios swift sorting multidimensional-array filter

jI有一个带有子对象的对象数组。

我想根据子对象

将数组排序为数组数组

例如:

的数组
[employee, employee, employee, employee]

现在我要做的就是这样排序 - >

[[employee.job.jobName = "baker",employee.job.jobName = "baker"],
[employee.job.jobName = "sweeper",employee.job.jobName = "sweeper"]]

目标是创建一个tableview,其中section标题将其job.jobName相同的员工分组。行将由这些员工,他们的名字等组成。为了做到这一点,我需要一组独特的job.jobNames,用于组成部分计数的员工,然后每个部分中的员工阵列组成行数。

我尝试过使用array.map和array.filter,但我认为我需要一个组合,我似乎无法解决这个问题。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

以下是我如何将员工数组拆分为数组数组的方法。

// Just needed to make the example work
struct Job {
    var name: String
}

struct Employee {
    var job: Job
}
/////////////////////////////////////////

let employees = [Employee(job: Job(name: "baker")), Employee(job: Job(name: "sweeper")), Employee(job: Job(name: "baker")), Employee(job: Job(name: "sweeper"))]

var jobs = Set<String>()

employees.forEach { jobs.insert($0.job.name) }

let sortedJobs = jobs.sort()

var groupedEmployees = [[Employee]]()
for jobName in sortedJobs {
    groupedEmployees.append(employees.filter({ $0.job.name == jobName }))
}

print(groupedEmployees)

它产生所需的输出:

[[main.Employee(job: main.Job(name: "baker")), main.Employee(job: main.Job(name: "baker"))], [main.Employee(job: main.Job(name: "sweeper")), main.Employee(job: main.Job(name: "sweeper"))]]

答案 1 :(得分:0)

import UIKit

/*Data Structure for Employee*/
class Employee:NSObject{
    /*Unemployed for default*/
    var job:String = "unemployed"

    init(job:String){
        self.job = job
    }

    override var description: String {
       return self.job
    }
}



/**Returns the array of arrays of enmployees sorted by jobs*/
func sortEmployees(employees:[Employee])->[[Employee]]
{
    //Sorting them by job
    let sorted = employees.sort({$0.job < $1.job})

    //Empty array of arrays
    var sortedEmployees = [[Employee]]()

    //job key
    var currentKey = ""
    for emp in sorted{
        let tempKey = emp.job
        //first element - edge case
        if sortedEmployees.count == 0
        {
            currentKey = tempKey
            sortedEmployees.append([emp])
        }
        else{
            if tempKey != currentKey{
                //create a new object
                sortedEmployees.append([emp])
                currentKey = tempKey
            }
            else{
                //get last array
                if let  lastArray = sortedEmployees.last{

                    var tempLast = lastArray
                    tempLast.append(emp)
                    sortedEmployees.removeLast()

                    sortedEmployees.append(tempLast)
                }
            }
        }
    }

    print(sortedEmployees)
    return sortedEmployees
}


/**Sample iVars*/
let emp1 = Employee(job: "baker")
let emp2 = Employee(job: "baker")
let emp3 = Employee(job: "sweeper")
let emp4 = Employee(job: "sweeper")


//Array with all of them
let array = [emp1, emp2, emp3, emp4]


let result = sortEmployees(array)
print(result)