Swift:按名称(字符串)的字母顺序将对象数组映射到新数组中的独立字母集合中 [英] Swift: Map Array of Objects Alphabetically by Name(String) into Separate Letter Collections within a new Array

查看:126
本文介绍了Swift:按名称(字符串)的字母顺序将对象数组映射到新数组中的独立字母集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为Contact的结构,该结构代表了一个人的接触,当前在数组中有一些接触。它们已经按字母顺序排序,但是我想通过name属性按字母顺序对它们进行排序,但这是一个字符串,但我不只是希望将它们按顺序排列在单个数组中,我想将对象分成不同的集合,由他们名字的首字母对应。例如。 A包含2个对象,其中联系人名称以A开头, B表示Bobby,Brad等名称,依此类推等等。

I have created a struct called Contact which represents a human contact where there are currently a few in an Array. They are already sorted alphabetically however I would like to sort them alphabetically by the name property which is a String BUT I don't just want to have them in order in a single array, I would like to split the objects out into different collections which is corresponded by the first letter of their name. eg. "A" contains 2 objects where a Contacts name begins with A, "B" for names like Bobby, Brad etc.. and so on and so forth.

let contactData:[Contact] = [
  Contact(id: 1, available: true, name: "Adam"),
  Contact(id: 2, available: true, name: "Adrian"),
  Contact(id: 3, available: true, name: "Balthazar"),
  Contact(id: 4, available: true, name: "Bobby")
]

I想要创建类似

let sectionTitles = ["A", "B"]
let sortedContactData = [
  [
    Contact(name: "Adam"),
    Contact(name: "Adrian")
  ],
  [
     Contact(name:"Balthazar")
     Contact(name:"Bobby")
  ]         
]

或类似的东西...

最终结果是我想将它们显示在UITableView中,并将Sections中的字母和Objects放入indexPath。行非常类似于iPhone本身的联系人应用程序如何执行。我实际上不确定这是否是实现此结果的最理想方法,所以我欢迎对此问题提出任何挑战!

The end result is that I would like to display them into a UITableView with the letters in Sections and the Objects into indexPath.rows much like how the Contacts app native to the iPhone does it. I am actually not sure whether this is the most ideal way to achieve this result so I welcome any challenges to this question!

推荐答案

let sortedContacts = contactData.sorted(by: { $0.name < $1.name }) // sort the Array first.
print(sortedContacts)

let groupedContacts = sortedContacts.reduce([[Contact]]()) {
    guard var last = $0.last else { return [[$1]] }
    var collection = $0
    if last.first!.name.characters.first == $1.name.characters.first {
        last += [$1]
        collection[collection.count - 1] = last
    } else {
        collection += [[$1]]
    }
    return collection
}
print(groupedContacts)




  1. 对列表进行排序。 O(nlogn),其中n是数组(contactData)中的项目数。

  2. 使用 reduce 迭代每个联系人
    ,然后将其添加到新组或最后一个组中。 O(n),其中n是数组中的项目数(sortedContacts)。

  1. sort the list. O(nlogn) , where n is the number of items in the Array(contactData).
  2. use reduce to iterate each contact in the list, then either add it to new group, or the last one. O(n), where n is the number of items in the Array(sortedContacts).

如果需要打印效果更好信息,最好使Contact符合协议 CustomStringConvertible

If you need to have a better printed information, you better make Contact conforms to protocol CustomStringConvertible

这篇关于Swift:按名称(字符串)的字母顺序将对象数组映射到新数组中的独立字母集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆