按某个属性对数组元素进行分组

28

我有一个对象数组,每个对象都有一个date属性。

我想创建一个新的数组,该数组包含多个子数组,每个子数组都包含具有相同日期的对象。

我知道需要使用.filter函数过滤对象,然后使用.map函数将对象添加到数组中。

但是如何告诉.map为每个分组创建一个单独的数组,并将此数组添加到"全局"数组中?同时如何告诉.filter只选择具有相同日期的对象?


可能是如何在Swift中按数组元素分组的重复问题。 - Cœur
7个回答

49

可能有点晚了,但新的Xcode 9 SDK字典有新的初始化方法。

init<S>(grouping values: S, by keyForValue: (S.Element) throws -> Key) rethrows where Value == [S.Element], S : Sequence

文档中提供了这个方法的简单示例。下面是我贴出的示例:

let students = ["Kofi", "Abena", "Efua", "Kweku", "Akosua"]
let studentsByLetter = Dictionary(grouping: students, by: { $0.first! })

结果将是:

["E": ["Efua"], "K": ["Kofi", "Kweku"], "A": ["Abena", "Akosua"]]

简单而优雅的解决方案! - Dhaval H. Nena

20

改进东方解决方案,以允许对任何内容进行有序分组:

extension Sequence {
    func group<GroupingType: Hashable>(by key: (Iterator.Element) -> GroupingType) -> [[Iterator.Element]] {
        var groups: [GroupingType: [Iterator.Element]] = [:]
        var groupsOrder: [GroupingType] = []
        forEach { element in
            let key = key(element)
            if case nil = groups[key]?.append(element) {
                groups[key] = [element]
                groupsOrder.append(key)
            }
        }
        return groupsOrder.map { groups[$0]! }
    }
}

然后它将适用于任何元组结构体,并适用于任何属性:

let a = [(grouping: 10, content: "a"),
         (grouping: 20, content: "b"),
         (grouping: 10, content: "c")]
print(a.group { $0.grouping })

struct GroupInt {
    var grouping: Int
    var content: String
}
let b = [GroupInt(grouping: 10, content: "a"),
         GroupInt(grouping: 20, content: "b"),
         GroupInt(grouping: 10, content: "c")]
print(b.group { $0.grouping })

不错!这是该函数的自然进展。我已经更新了我的解决方案,以允许可选的分组键。这个功能在你的解决方案中也会很方便。 - JERSH

8
使用Swift 5,你可以通过Dictionaryinit(grouping:by:)初始化器将一个数组的元素按照其属性之一分组成字典。完成后,你可以使用Dictionaryvalues属性和Arrayinit(_:)初始化器,创建一个由数组组成的新数组。


以下Playground示例代码展示了如何将一个数组的元素按照其中一个属性分组为一个新的数组:
import Foundation

struct Purchase: CustomStringConvertible {
    let id: Int 
    let date: Date
    var description: String {
        return "Purchase #\(id) (\(date))"
    }
}

let date1 = Calendar.current.date(from: DateComponents(year: 2010, month: 11, day: 22))!
let date2 = Calendar.current.date(from: DateComponents(year: 2015, month: 5, day: 1))!
let date3 = Calendar.current.date(from: DateComponents(year: 2012, month: 8, day: 15))!
let purchases = [
    Purchase(id: 1, date: date1),
    Purchase(id: 2, date: date1),
    Purchase(id: 3, date: date2),
    Purchase(id: 4, date: date3),
    Purchase(id: 5, date: date3)
]

let groupingDictionary = Dictionary(grouping: purchases, by: { $0.date })
print(groupingDictionary)
/*
 [
    2012-08-14 22:00:00 +0000: [Purchase #4 (2012-08-14 22:00:00 +0000), Purchase #5 (2012-08-14 22:00:00 +0000)],
    2010-11-21 23:00:00 +0000: [Purchase #1 (2010-11-21 23:00:00 +0000), Purchase #2 (2010-11-21 23:00:00 +0000)],
    2015-04-30 22:00:00 +0000: [Purchase #3 (2015-04-30 22:00:00 +0000)]
 ]
 */

let groupingArray = Array(groupingDictionary.values)
print(groupingArray)
/*
 [
    [Purchase #3 (2015-04-30 22:00:00 +0000)],
    [Purchase #4 (2012-08-14 22:00:00 +0000), Purchase #5 (2012-08-14 22:00:00 +0000)],
    [Purchase #1 (2010-11-21 23:00:00 +0000), Purchase #2 (2010-11-21 23:00:00 +0000)]
 ]
 */

2

抽象化一步,您想要按某个属性对数组元素进行分组。您可以让一个映射来为您进行分组,如下所示:

protocol Groupable {
    associatedtype GroupingType: Hashable
    var grouping: GroupingType { get set }
}

extension Array where Element: Groupable  {
    typealias GroupingType = Element.GroupingType

    func grouped() -> [[Element]] {
        var groups = [GroupingType: [Element]]()

        for element in self {
            if let _ = groups[element.grouping] {
                groups[element.grouping]!.append(element)
            } else {
                groups[element.grouping] = [element]
            }
        }

        return Array<[Element]>(groups.values)
    }
}

请注意,这种分组是稳定的,也就是说,分组按照出现的顺序排列,而在分组内,各个元素的顺序与原始数组中的顺序相同。

用法示例

我将使用整数来举例说明;对于任何(可哈希)类型的T,包括Date,都应该很清楚如何使用。

struct GroupInt: Groupable {
    typealias GroupingType = Int
    var grouping: Int
    var content: String
}

var a = [GroupInt(grouping: 1, content: "a"),
         GroupInt(grouping: 2, content: "b") ,
         GroupInt(grouping: 1, content: "c")]

print(a.grouped())
// > [[GroupInt(grouping: 2, content: "b")], [GroupInt(grouping: 1, content: "a"), GroupInt(grouping: 1, content: "c")]]

顺便提一下,在扩展中的“typealias”应该是不必要的,但编译器没有推断出“groups”的类型。 (报告 - Raphael

2

Rapheal的解决方案确实有效。然而,我建议修改解决方案以支持分组实际上是稳定的这一声明。

目前情况下,调用grouped()将返回一个已分组的数组,但是后续的调用可能会返回一个顺序不同的组数组,尽管每个组的元素都按预期顺序排列。

internal protocol Groupable {
    associatedtype GroupingType : Hashable
    var groupingKey : GroupingType? { get }
}

extension Array where Element : Groupable {

    typealias GroupingType = Element.GroupingType

    func grouped(nilsAsSingleGroup: Bool = false) -> [[Element]] {
        var groups = [Int : [Element]]()
        var groupsOrder = [Int]()
        let nilGroupingKey = UUID().uuidString.hashValue
        var nilGroup = [Element]()

        for element in self {

            // If it has a grouping key then use it. Otherwise, conditionally make one based on if nils get put in the same bucket or not
            var groupingKey = element.groupingKey?.hashValue ?? UUID().uuidString.hashValue
            if nilsAsSingleGroup, element.groupingKey == nil { groupingKey = nilGroupingKey }

            // Group nils together
            if nilsAsSingleGroup, element.groupingKey == nil {
                nilGroup.append(element)
                continue
            }

            // Place the element in the right bucket
            if let _ = groups[groupingKey] {
                groups[groupingKey]!.append(element)
            } else {
                // New key, track it
                groups[groupingKey] = [element]
                groupsOrder.append(groupingKey)
            }

        }

        // Build our array of arrays from the dictionary of buckets
        var grouped = groupsOrder.flatMap{ groups[$0] }
        if nilsAsSingleGroup, !nilGroup.isEmpty { grouped.append(nilGroup) }

        return grouped
    }
}

现在我们追踪发现新分组的顺序,因此可以更加一致地返回一个分组数组,而不仅仅依靠字典的无序 values 属性。

struct GroupableInt: Groupable {
    typealias GroupingType = Int
    var grouping: Int?
    var content: String
}

var a = [GroupableInt(groupingKey: 1, value: "test1"),
         GroupableInt(groupingKey: 2, value: "test2"),
         GroupableInt(groupingKey: 2, value: "test3"),
         GroupableInt(groupingKey: nil, value: "test4"),
         GroupableInt(groupingKey: 3, value: "test5"),
         GroupableInt(groupingKey: 3, value: "test6"),
         GroupableInt(groupingKey: nil, value: "test7")]

print(a.grouped())
// > [[GroupableInt(groupingKey: 1, value: "test1")], [GroupableInt(groupingKey: 2, value: "test2"),GroupableInt(groupingKey: 2, value: "test3")], [GroupableInt(groupingKey: nil, value: "test4")],[GroupableInt(groupingKey: 3, value: "test5"),GroupableInt(groupingKey: 3, value: "test6")],[GroupableInt(groupingKey: nil, value: "test7")]]

print(a.grouped(nilsAsSingleGroup: true))
// > [[GroupableInt(groupingKey: 1, value: "test1")], [GroupableInt(groupingKey: 2, value: "test2"),GroupableInt(groupingKey: 2, value: "test3")], [GroupableInt(groupingKey: nil, value: "test4"),GroupableInt(groupingKey: nil, value: "test7")],[GroupableInt(groupingKey: 3, value: "test5"),GroupableInt(groupingKey: 3, value: "test6")]]

我已更新此内容以支持可选分组键。现在,grouped函数接受一个Bool参数,指定是否将nil分组为单个组或按照它们被发现的方式单独分组,后者是默认值(false)。这里是它的要点。 - JERSH

1
+1 对GolenKovkosty答案的支持。
init<S>(grouping values: S, by keyForValue: (S.Element) throws -> Key) rethrows where Value == [S.Element], S : Sequence

更多例子:
enum Parity {
   case even, odd
   init(_ value: Int) {
       self = value % 2 == 0 ? .even : .odd
   }
}
let parity = Dictionary(grouping: 0 ..< 10 , by: Parity.init )

等同于。
let parity2 = Dictionary(grouping: 0 ..< 10) { $0 % 2 }

在您的情况下:

struct Person : CustomStringConvertible {
    let dateOfBirth : Date
    let name :String
    var description: String {
        return "\(name)"
    }
}

extension Date {
    init(dateString:String) {
        let formatter = DateFormatter()
        formatter.timeZone = NSTimeZone.default
        formatter.dateFormat = "MM/dd/yyyy"
        self = formatter.date(from: dateString)!
    }
}
let people = [Person(dateOfBirth:Date(dateString:"01/01/2017"),name:"Foo"),
              Person(dateOfBirth:Date(dateString:"01/01/2017"),name:"Bar"),
              Person(dateOfBirth:Date(dateString:"02/01/2017"),name:"FooBar")]
let parityFields = Dictionary(grouping: people) {$0.dateOfBirth}

输出:

[2017-01-01: [Foo, Bar], 2017-02-01:  [FooBar] ]

1

这是一种清晰的分组方法:

let grouped = allRows.group(by: {$0.groupId}) // Dictionary with the key groupId

假设您有一个联系人数组,例如:
class ContactPerson {
    var groupId:String?
    var name:String?
    var contactRecords:[PhoneBookEntry] = []
}

为了实现这一点,请添加此扩展:

class Box<A> {
    var value: A
    init(_ val: A) {
        self.value = val
    }
}

public extension Sequence {
    func group<U: Hashable>(by key: (Iterator.Element) -> U) -> [U: [Iterator.Element]] {
        var categories: [U: Box<[Iterator.Element]>] = [:]
        for element in self {
            let key = key(element)
            if case nil = categories[key]?.value.append(element) {
                categories[key] = Box([element])
            }
        }
        var result: [U: [Iterator.Element]] = Dictionary(minimumCapacity: categories.count)
        for (key, val) in categories {
            result[key] = val.value
        }
        return result
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接